LINQ连接表并按错误分组

时间:2018-06-28 08:38:04

标签: asp.net entity-framework linq join group-by

我需要连接两个表并按其列分组。我也需要选择总和之一的列。我已经尝试了另一个问题的解决方案,但是该解决方案并未说明如何使用sum()做这件事,这是我需要选择的最重要的专栏

这是我的SQL语句。我已经尝试过了,它返回了我期望的结果

SELECT lt.Name, SUM(l.QuotaDays)
FROM EmployeeLeaveQuota l
JOIN EmployeeLeaveQuotaType lt ON l.EmployeeLeaveQuotaTypeId =     lt.EmployeeLeaveQuotaTypeId
WHERE l.EmployeeId = '12345678'
GROUP BY l.EmployeeLeaveQuotaTypeId, lt.Name

这是我的LINQ

var leaveQuota = await (from l in DB.EmployeeLeaveQuota
                                join lt in DB.EmployeeLeaveQuotaType on l.EmployeeLeaveQuotaTypeId equals lt.EmployeeLeaveQuotaTypeId
                                where l.EmployeeId == employeeId
                                where l.ValidUntil >= DateTime.UtcNow
                                group new { l, lt } by new
                                {
                                    l.EmployeeLeaveQuotaTypeId,
                                    l.QuotaDays,
                                    lt.Name
                                } into g
                                select new LeaveTypeModel
                                {
                                    EmployeeLeaveQuotaTypeId = g.Key.EmployeeLeaveQuotaTypeId,
                                    LeaveName = g.Key.Name,
                                    QuotaDays = g.Sum(x => x.l.QuotaDays)
                                }).ToListAsync();

但是当我跑步时,它会出错

  

InvalidOperationException:为警告'Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning生成的错误:LINQ表达式'GroupBy(new <> f__AnonymousType1 3(EmployeeLeaveQuotaTypeId = [l].EmployeeLeaveQuotaTypeId, QuotaDays = [l].QuotaDays, Name = [lt].Name), new <>f__AnonymousType0 2(l = [l],lt = [lt]))'不翻译,将在本地进行评估。”。可以通过将事件ID“ RelationalEventId.QueryClientEvaluationWarning”传递到“ DbContext.OnConfiguring”或“ AddDbContext”中的“ ConfigureWarnings”方法来抑制或记录此异常。

有人知道为什么吗?请帮忙 谢谢

0 个答案:

没有答案