c# - 需要帮助在 C# 中进行分组
问题描述
我有以下代码按发票号码分组付款金额和
- 如果类型 1 或 2 我想显示
sum(Amount)
为Interest
, - 如果类型 3 或 4 我想显示
sum(Amount)
为Principal
我尝试将以下代码分组,InvoiceNumber
如下所示,但出现InvalidOperationException: The LINQ expression GroupByShaperExpression'
异常。
我要完成
Type Amount InvoiceNumber
1 $100 123
2 $50 123
3 $100 123
4 $1200 123
1 $100 124
1 $300 124
3 $100 124
3 $300 124
4 $100 124
我想按发票编号分组并将类型 = 1&2 的金额字段的值相加并显示为利息,将 3&4 显示为 Princepal
InvoiceNumber Interest Princepal
123 $150 $1300
124 $400 $500
解决方案
有几个问题:
- EF Core 之后无法访问导航属性
GroupBy
- 即使 EF Core 可以翻译此查询,它也是无效的。
考虑用以下方式重写查询:
var ProductPaymentIDs = ProductPayments.Select(pp => pp.ID).ToList();
var query =
from cl in Context.PaymentCodingLines
where ProductPaymentIDs.Contains(cl.ProductPaymentID)
group new { cl.InvoiceLineNav.TypeID, cl.Amount } by g.InvoiceLineNav.InvoiceNav.InvoiceNumber into g
select new DetailDTO
{
InvoiceNumber = g.Key,
InterestPaid = g.Sum(x => x.TypeID == 1 || x.TypeID == 2 ? x.Amount : 0),
PrincipalPaid = g.Sum(x => x.TypeID == 3 || x.TypeID == 4 ? x.Amount : 0)
};
var Details = query.ToList();
推荐阅读
- c++ - C++ 存储和更新已排序项目的有效方法
- terminal - 在 Mac OSX Sierra 上使用 Homebrew 安装 gedit
- windows - 如何在 Windows 10 的音乐中编辑 MP3、FLAC 等文件的标题、贡献艺术家、专辑、年份等信息
- asp.net-web-api - 有没有办法从同一网络的多个设备中检索 cookie?
- assembly - 当我在 C 中调用 ASM 函数时,参数不会出现在堆栈上
- firebase - 我遇到了一个问题,当我尝试将项目链接到 Firebase 时,我看到了这个错误
- c# - 转换为正确的 DateTime 格式时的歧义
- amazon-web-services - 将多台机器上的远程文件夹同步到一个 AWS 实例
- javascript - React reduce、replace 和 wrap in span
- java - com.google.firebase.database.DatabaseException: Can't convert of object of type java.lang.String to type com.NoteModelActivity 突然出现错误