我会要求有人请检查我的 LINQ查询 4个表加入和分组在逻辑上是否正确?是否正确编写4个表join和Group by子句的LINQ查询?
我的第二点:
如果我评论第TotalTime = tstGroup.Sum(x=>x.Duration)
行,则效果正常。
但是,如果我取消注释这一行,它会说出以下异常。
A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll
有什么问题?请评论
表格
Table A: Id, FirstName
Table B: Id, AId(Table A FK)
Table C: Id, BId(Table B FK), Duration
Table D: Id, AId(Table A FK). Description
tstData= (from a in _context.A
join b in _context.B on a.Id equals b.AId
join c in _context.C on b.Id equals c.BId
join d in _context.D on a.Id equals d.AId
group c by new { a= a, b= b, d= d} into tstGroup
select new myobj
{
FirstName= tstGroup.Key.a.FirstName,
Description= tstGroup.Key.d.description,
TotalTime = tstGroup.Sum(x=>x.Duration),
}).ToList();
修改后的LINQ。 你觉得现在是正确的吗?删除了多个组条款。
tstData= (from a in _context.A
join b in _context.B on a.Id equals b.AId
join c in _context.C on b.Id equals c.BId
join d in _context.D on a.Id equals d.AId
group c by new { c.Id, c.Duration, d.Name, a.FirstName} into tstGroup
select new myobj
{
FirstName= tstGroup.Key.FirstName,
Description= tstGroup.Key.description,
TotalTime = tstGroup.Sum(x=>x.Duration),
}).ToList();