C# 帮助我使用EntityFramework将SQL转换为linq嵌套lambda表达式
我试图从SQL中获取一些数据,但我无法使用Linq,在T-SQL中,这项工作:C# 帮助我使用EntityFramework将SQL转换为linq嵌套lambda表达式,c#,linq,entity-framework-4,lambda,sql-to-linq-conversion,C#,Linq,Entity Framework 4,Lambda,Sql To Linq Conversion,我试图从SQL中获取一些数据,但我无法使用Linq,在T-SQL中,这项工作: select * from MTRBatch MB Inner Join MTR M on MB.Id = M.MTRBatchId Inner JOIN MTRHeats MH on M.Id = MH.MTRId LEFT OUTER JOIN Vendor V on MB.VendorId = v.Id Inner Join Manufacturer MF on MB.ManufacturerId = MF.I
select *
from MTRBatch MB
Inner Join MTR M on MB.Id = M.MTRBatchId
Inner JOIN MTRHeats MH on M.Id = MH.MTRId
LEFT OUTER JOIN Vendor V on MB.VendorId = v.Id
Inner Join Manufacturer MF on MB.ManufacturerId = MF.Id
Where MB.ManufacturerId = 1
AND MH.Heat = 'z01'
除了那个过滤器,我需要所有的树
我尝试了这个,但没有成功:
MTRBatches
.Include(x => x.MTRs.Select(m => m.MTRHeats))
.Include(x => x.Manufacturer)
.Include(x => x.Vendor)
.Where(x => (x.Manufacturer.Id == 1));
.Where(x => x.MTRs.Any(m => m.MTRHeats.Any(h => h.Heat == 'z01')));
这应该会有所帮助
dataContext
是实体框架容器的实例名
var result = dataContext.MTRBatches
.Join(dataContext.MTRs,
mb => mb.Id,
mtr => mtr.MTRBatchId,
(mb, mtr) => new{ Batch = mb, MTR = mtr })
.Join(dataContext.MTRHeats,
x => x.MTR.Id,
mh => mh.MTRId,
(x, mh) => new{ Batch = x.Batch, MTR = x.MTR, Heat = mh })
.Join(dataContext.Vendors.DefaultIfEmpty(),
x => x.Batch.VendorId,
v => v.Id,
(x, v) => new{ Batch = x.Batch, MTR = x.MTR, Heat = x.Heat, Vendor = v })
.Join(dataContext.Manufacturers,
x => x.Batch.ManufacturerId,
mf => mf.Id,
(x, mf) => new{ Batch = x.Batch, MTR = x.MTR, Heat = x.Heat, Vendor = x.Vendor, Manufacturer = mf})
.Where(x => x.Manufacturer.Id == 1 && x.Heat.Heat == "z01");
没有像在。。。?错误的结果?撞车烧伤?没有编译?结果不正确:它给我带来了所有的MTR,就像MH。热过滤器没有发生一样