繁体   English   中英

在 C# 中需要帮助分组

[英]Need Help Grouping in C#

我有以下代码可以按发票编号和

  • 如果输入 1 或 2 我想将sum(Amount)显示为Interest
  • 如果输入 3 或 4,我想将sum(Amount)显示为Principal

我尝试了以下代码按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();

异常错误

休息服务失败:URL:带有状态代码 InternalServerError 的 url。 错误内容: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)' 无法翻译。 以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038 在..

我想完成

 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM