select 如何使用实体的 linq 或 EF Core 2 从子级到父级的层次结构?
How select hierarchy from child to parent using linq for entities or EF Core 2?
有四个实体(模型)的层次结构。所有实体都具有一对多的关系。它看起来如下:
public class Region
{
public Guid Id { get; set; }
public int RegionId { get; set; }
public virtual ICollection<District> Districts { get; set; }
}
public class District
{
public Guid Id { get; set; }
public int DistrictId { get; set; }
public virtual ICollection<Building> Buildings { get; set; }
public int RegionId { get; set; }
public Region Region { get; set; }
}
public class Building
{
public Guid Id { get; set; }
public int BuildingId { get; set; }
public int DistrictId { get; set; }
public virtual ICollection<Flat> Flats { get; set; }
public District District { get; set; }
}
public class Flat
{
public Guid Id { get; set; }
public int FlatId { get; set; }
public int BuildingId { get; set; }
public Building Building { get; set; }
}
它是这样的层次结构:Region->District->Building->Flat。
我想要 return 的 Flat
集合,其中包括顶部的所有相关实体。
我这样做如下:
var flat = _context.Flats
.Include(t => t.Building)
.Include(b=>b.Building.District)
.Include(b => b.Building.District.Region).GetPaged(PageNumber, 20);
但由于某种原因 return 打破了结果....
[{"id":"6cbb1431-36d4-490c-a047-c211e4db3bdd","flatId":29570,"roomsCount":1,"totalArea":41.95,"kitchenArea":11.85,"floor":21,"buildingId":130,"price":5725650.00,"building":{"id":"03baca1e-02e9-4ee0-ae47-920e3eb0d5bb","buildingId":130,"name":"NNN","queue":1,"housing":"1","districtId":13003,"flats":[
由于某些原因,它在 public virtual ICollection<Flat> Flats { get; set; }
上中断了。我无法删除它,因为 FK 需要它。
以下查询有效:
_context.Flat
.Join(
_context.Building,
f => f.BuildingId,
b => b.BuildingId,
(f, b) =>
new
{
f = f,
b = b
}
)
.Join(
_context.District,
temp0 => temp0.b.DistrictId,
d => d.DistrictId,
(temp0, d) =>
new
{
temp0 = temp0,
d = d
}
)
.Join(
_context.Region,
temp1 => temp1.d.RegionId,
r => r.RegionId,
(temp1, r) =>
new
{
Id = temp1.temp0.f.Id,
FlatId = temp1.temp0.f.FlatId,
Floor = temp1.temp0.f.Floor,
RoomsCount = temp1.temp0.f.RoomsCount,
TotalArea = temp1.temp0.f.TotalArea,
KitchenArea = temp1.temp0.f.KitchenArea,
Price = temp1.temp0.f.Price,
Building = new
{
Id = temp1.temp0.b.Id,
BuildingId = temp1.temp0.b.Id,
Name = temp1.temp0.b.Name,
Queue = temp1.temp0.b.Queue,
Housing = temp1.temp0.b.Housing,
District = new
{
Id = temp1.d.Id,
DistrictId = temp1.d.DistrictId,
Name = temp1.d.Name,
Regions = new
{
Id = r.Id,
RegionId = r.Id,
Name = r.Name
}
}
}
}
).GetPaged(PageNumber, PageSize)
这就是return我想要的:
{
"id": "a49a0a48-370b-46da-b6da-ea4040cc1431",
"flatId": 84095,
"roomsCount": 3,
"totalArea": 76.5,
"kitchenArea": 11.07,
"floor": 18,
"buildingId": 0,
"price": 5567500,
"building": {
"id": "037c4074-3ff5-4fbd-92ea-88c8f746b883",
"buildingId": 200,
"name": "NNNN",
"queue": 3,
"housing": "4",
"districtId": 0,
"flats": null,
"district": {
"id": "db031e09-1d6c-41f4-9e45-ec6170e3df8b",
"districtId": 5005,
"name": "Distr",
"buildings": null,
"regionId": 0,
"region": {
"id": "5ac5766f-e62e-4da9-86cf-e4a85423503a",
"regionId": 5001,
"name": "Region",
"districts": null
}
}
}
}
但是看起来很麻烦。
我如何使用 EF 功能来做到这一点?或者也许以某种方式减少 linq?为什么 include()
return 打破结果?
P.S。 .GetPaged(PageNumber, 20);
是分页的扩展方法,他工作正常。
我的建议如下
var flag = (from f in _context.Flags
join b in _context.Buildings
on b.BuildingId equals f.BuildingId
join d in _context.Districts
on d.DistrictId equals b.DistrictId
join r in _context.Regions
on r.RegionId equals d.RegionId
select new
{ ...
}).GetPaged(PageNumber, 20);
您可以从建筑物、地区和地区中获取任何 属性 以填充到您的结果对象中。
有四个实体(模型)的层次结构。所有实体都具有一对多的关系。它看起来如下:
public class Region
{
public Guid Id { get; set; }
public int RegionId { get; set; }
public virtual ICollection<District> Districts { get; set; }
}
public class District
{
public Guid Id { get; set; }
public int DistrictId { get; set; }
public virtual ICollection<Building> Buildings { get; set; }
public int RegionId { get; set; }
public Region Region { get; set; }
}
public class Building
{
public Guid Id { get; set; }
public int BuildingId { get; set; }
public int DistrictId { get; set; }
public virtual ICollection<Flat> Flats { get; set; }
public District District { get; set; }
}
public class Flat
{
public Guid Id { get; set; }
public int FlatId { get; set; }
public int BuildingId { get; set; }
public Building Building { get; set; }
}
它是这样的层次结构:Region->District->Building->Flat。
我想要 return 的 Flat
集合,其中包括顶部的所有相关实体。
我这样做如下:
var flat = _context.Flats
.Include(t => t.Building)
.Include(b=>b.Building.District)
.Include(b => b.Building.District.Region).GetPaged(PageNumber, 20);
但由于某种原因 return 打破了结果....
[{"id":"6cbb1431-36d4-490c-a047-c211e4db3bdd","flatId":29570,"roomsCount":1,"totalArea":41.95,"kitchenArea":11.85,"floor":21,"buildingId":130,"price":5725650.00,"building":{"id":"03baca1e-02e9-4ee0-ae47-920e3eb0d5bb","buildingId":130,"name":"NNN","queue":1,"housing":"1","districtId":13003,"flats":[
由于某些原因,它在 public virtual ICollection<Flat> Flats { get; set; }
上中断了。我无法删除它,因为 FK 需要它。
以下查询有效:
_context.Flat
.Join(
_context.Building,
f => f.BuildingId,
b => b.BuildingId,
(f, b) =>
new
{
f = f,
b = b
}
)
.Join(
_context.District,
temp0 => temp0.b.DistrictId,
d => d.DistrictId,
(temp0, d) =>
new
{
temp0 = temp0,
d = d
}
)
.Join(
_context.Region,
temp1 => temp1.d.RegionId,
r => r.RegionId,
(temp1, r) =>
new
{
Id = temp1.temp0.f.Id,
FlatId = temp1.temp0.f.FlatId,
Floor = temp1.temp0.f.Floor,
RoomsCount = temp1.temp0.f.RoomsCount,
TotalArea = temp1.temp0.f.TotalArea,
KitchenArea = temp1.temp0.f.KitchenArea,
Price = temp1.temp0.f.Price,
Building = new
{
Id = temp1.temp0.b.Id,
BuildingId = temp1.temp0.b.Id,
Name = temp1.temp0.b.Name,
Queue = temp1.temp0.b.Queue,
Housing = temp1.temp0.b.Housing,
District = new
{
Id = temp1.d.Id,
DistrictId = temp1.d.DistrictId,
Name = temp1.d.Name,
Regions = new
{
Id = r.Id,
RegionId = r.Id,
Name = r.Name
}
}
}
}
).GetPaged(PageNumber, PageSize)
这就是return我想要的:
{
"id": "a49a0a48-370b-46da-b6da-ea4040cc1431",
"flatId": 84095,
"roomsCount": 3,
"totalArea": 76.5,
"kitchenArea": 11.07,
"floor": 18,
"buildingId": 0,
"price": 5567500,
"building": {
"id": "037c4074-3ff5-4fbd-92ea-88c8f746b883",
"buildingId": 200,
"name": "NNNN",
"queue": 3,
"housing": "4",
"districtId": 0,
"flats": null,
"district": {
"id": "db031e09-1d6c-41f4-9e45-ec6170e3df8b",
"districtId": 5005,
"name": "Distr",
"buildings": null,
"regionId": 0,
"region": {
"id": "5ac5766f-e62e-4da9-86cf-e4a85423503a",
"regionId": 5001,
"name": "Region",
"districts": null
}
}
}
}
但是看起来很麻烦。
我如何使用 EF 功能来做到这一点?或者也许以某种方式减少 linq?为什么 include()
return 打破结果?
P.S。 .GetPaged(PageNumber, 20);
是分页的扩展方法,他工作正常。
我的建议如下
var flag = (from f in _context.Flags
join b in _context.Buildings
on b.BuildingId equals f.BuildingId
join d in _context.Districts
on d.DistrictId equals b.DistrictId
join r in _context.Regions
on r.RegionId equals d.RegionId
select new
{ ...
}).GetPaged(PageNumber, 20);
您可以从建筑物、地区和地区中获取任何 属性 以填充到您的结果对象中。