请有人协助以下查询。我正在尝试为每个客户选择所有收据的总和。但是我的选择部分中的'Total ='子查询无效,我收到以下错误:
无法创建“AppName.Domain.Entities.AccountingEntry”类型的常量值。在此上下文中仅支持原始类型或枚举类型。
我已经尝试在该查询之后执行ToList(),但这会导致同样的问题。使用linq选择Sum作为子查询的标准方法是什么?
var receipts = (from ae in repo.AccountingEntries
join c in repo.Clients on ae.ClientId equals c.ClientId
join m in repo.Memberships on c.MembershipId equals m.MembershipId
where
(ae.EntryDate <= start) &&
(ae.ClientId != null) &&
(ae.AccountingEntryTypeId == (byte)Shared.AccountingEntryTypes.Receipt)
select new AppName.Reports.Clients.AgeAnalysis.Receipt
{
ClientId = (Guid)ae.ClientId,
Client = c.FirstName + " " + c.LastName,
Membership = c.Membership.Name,
Total = (from ae2 in repo.AccountingEntries where ae2.ClientId == ae.ClientId select ae2.Total).Sum()
});
谢谢, 加里
答案 0 :(得分:2)
这似乎过于复杂和不必要。如果您的实体模型设置正确,则可以通过删除连接并使用“group by”子句来简化。有点像:
from ae in c.AccountingEntries
where
(ae.EntryDate <= start) &&
(ae.ClientId != null ) &&
(ae.AccountingEntryTypeId == (byte)Shared.AccountEntyrTypes.Receipt)
group by
ae.Client into g
select
new AppName.Reports.Clients.AgeAnalysis.Receipt
{
ClientId = (Guid)g.Key.ClientId,
Client = g.Key.FirstName + " " + g.Key.LastName,
Membership = g.Key.Membership.Name,
Total = g.Sum( p => p.Total )
}