[英]Need Help Grouping in C#
I have the following code to group payment amount by Invoice Number and我有以下代码可以按发票编号和
sum(Amount)
as Interest
,如果输入 1 或 2 我想将sum(Amount)
显示为Interest
,sum(Amount)
as Principal
如果输入 3 或 4,我想将sum(Amount)
显示为Principal
I tried the following code to Group by InvoiceNumber
as shown below but i get InvalidOperationException: The LINQ expression GroupByShaperExpression'
exception.我尝试了以下代码按InvoiceNumber
,如下所示,但我得到InvalidOperationException: The LINQ expression GroupByShaperExpression'
异常。
var payments = await _dbContext.ProductPayments.AsNoTracking()
.Where(pp => pp.PaymentID == request.PaymentID)
.ToListAsync();
var ProductPaymentIDs = ProductPayments.Select(pp => pp.ID).ToList();
var Details = Context.PaymentCodingLines.AsNoTracking()
.Include(cl => cl.ProductPaymentNav)
.Where(cl => ProductPaymentIDs.Contains(cl.ProductPaymentID))
.GroupBy(g => new
{
InvoiceNumber = g.InvoiceLineNav.InvoiceNav.InvoiceNumber
})
.Select(s => new DetailDTO
{
InterestPaid = s.Where(pp => pp.InvoiceLineNav.TypeID == 1|| pp.InvoiceLineNav.TypeID == 2).Sum(a => a.Amount),
PrincipalPaid = s.Where(pp => pp.InvoiceLineNav.TypeID == 3 || pp.InvoiceLineNav.TypeID == 4).Sum(a => a.Amount)
})
.ToList();
Exception Error异常错误
Rest Service FAILED: URL: url with status code InternalServerError.休息服务失败:URL:带有状态代码 InternalServerError 的 url。 Error Content: System.InvalidOperationException: The LINQ expression '(GroupByShaperExpression: KeySelector: new { InvoiceNumber = (i.InvoiceNumber) }, ElementSelector:(EntityShaperExpression: EntityType: PaymentCodingLine ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ) .Where(pp => pp.InvoiceLineNav.TypeID == __TypeKey_2 || pp.InvoiceLineNav.TypeID == __TypeKey_3)' could not be translated.错误内容:System.InvalidOperationException: The LINQ expression '(GroupByShaperExpression: KeySelector: new { InvoiceNumber = (i.InvoiceNumber) }, ElementSelector:(EntityShaperExpression: EntityType: PaymentCodingLine ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: FalseWhere) pp => pp.InvoiceLineNav.TypeID == __TypeKey_2 || pp.InvoiceLineNav.TypeID == __TypeKey_3)' 无法翻译。 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038 。 at..在..
I want to accomplish我想完成
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
I want to group by invoice Number and Sum the value of Amount field for Type = 1&2 and display as Interest and 3&4 as Princepal我想按发票编号分组并汇总类型 = 1&2 的金额字段的值,并显示为利息,将 3&4 显示为 Princepal
InvoiceNumber Interest Princepal
123 $150 $1300
124 $400 $500
There are several problems:有几个问题:
GroupBy
EF Core 无法访问GroupBy
后的导航属性Consider to rewrite the query in the following way:考虑以下列方式重写查询:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.