c# - Linq 或 EF - 使用过滤属性多次加入非匿名对象
问题描述
我正在使用 EF,不幸的是 includefiltered 不是选项。所以我必须以某种方式重写我的代码并从中创建非匿名对象。我决定重写它加入,但它可以是任何工作。
我有实体,简化版 Car.Tires.Manufacturers。汽车可以有零到多个轮胎,轮胎可以有零到多个制造商
我想获得具有特定 ID 的汽车,并且只有特定制造商的轮胎。问题是我的结果汽车的轮胎总是有零制造商。
我当前的代码是:
var car1 = (from c in this.dbContext.Cars
.Include(cr => cr.Tires)
.ThenInclude(crt => crt.Manufacturers)
join t in this.dbContext.Tires
.Include(ct => ct.Manufacturers)
on c.ID equals t.CarID into carTires
from t in carTires.DefaultIfEmpty()
join m in this.dbContext.Manufacturers on t.ManufacturerID equals m.ID into completeSet
from cs in completeSet.DefaultIfEmpty()
where (c.ID == someCarID ) // and later I will add filter for tire's manufacturer
select new Car
{
ID = c.ID,
Tires = c.Tires
}
如果我使用代码
var car2 = this.dbContext.Cars
.Include(c => c.Tires)
.ThenInclude(t => t.Manufacturers)
Where(c => c.ID == someCarID)
在 Car2 中有一些制造商。
为什么car1轮胎的制造商为空,如何解决?
注意:这是中间目标。我的最终目标是只为选定的制造商购买带轮胎的汽车。
解决方案
尝试:
var manufacturerTires = dbContext.Tires.Where(t => t.ManufacturerID == someManufacturerID);
var carTires = dbContext.Cars.
Where(car => car.ID == someCarID)
.Join(manufacturerTires,
car => car.ID,
tire => tire.CarID,
(car, tire) => new { car, tire })
.ToList();
这应该返回一个匿名对象new { Car, Tire }
如果我们需要获取 Car 和 Car.Tires 的现有结构,我们可以在上述查询的末尾添加一个 GroupBy,例如:
.GroupBy(c => c.car, c => c.tire, (car, tires) => new Car{ ID = car.ID, Tires = tires});
//this could be an expensive query as the GroupBy is on all the columns in Car table
推荐阅读
- backup - rclone 使用过多的下载带宽
- json - 如果存在则从 json 返回特定值,如果不存在则返回零行
- c# - 如何 PInvoke 返回接口的 C++ 函数
- python - 无法抓取网站
- spring-boot - 我使用spring data rest或spring data jpa accosiation在子表中获取空值作为父表的外键
- json - 如何将 MSSQL 查询输出保存到 .txt 或 .json 文件?
- android - 在 CardView 中对齐 TextView
- java - InvalidClassException:无法将非枚举描述符绑定到枚举类
- android - getLastSignedInAccount NULL 异常
- selenium - 如何使用 selenium 抓取使用 PDF.js 创建的 PDF?