【问题标题】:Linq Join with GroupBy and SumLinq Join 与 GroupBy 和 Sum
【发布时间】:2023-03-30 02:33:01
【问题描述】:

我有如下的交易表和客户表:

public class Customer
{    
    public string Id{ get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
 }

public class SalesTransaction
{
    public int Id { get; set; }

    public decimal Amount { get; set; }
    public string CustomerId{ get; set; }
}

现在我需要获取每个客户的交易总金额列表,并在列表中显示客户名称和总交易金额

我试过下面的 linq 方法语法

await _context.SalesTransactions
            .GroupBy(w=>w.CustomerId)
            .OrderByDescending(g=>g.Sum(t=>t.Amount))
            .ToListAsync();

但是当我尝试运行它时,我收到以下错误

InvalidOperationException:不支持客户端 GroupBy。

我也试过下面的查询语法

var TransactionSummary = await (from w in _context.WalletTransactions
                           //join c in _context.Customers 
                            on w.CustomerId equals c.Id
                            group w by w.CustomerId
                            into ct
                            //from c in ct.DefaultIfEmpty()
                            select new
                            {
                             ID=ct.Key,
                             TransactionAmount=ct.Sum(a=>a.Amount),
                            // ct.Name
                             }).ToListAsync();

但 Sum(w.Amount) 显示错误,提示“当前上下文中不存在总和”。

我也不确定在查询语法中的何处放置分组子句以实现分组 Customer.Id 字段的结果。

请注意,我注释掉的行是我希望添加的子句,但不确定在何处以及如何正确添加它们

我希望找到正确的方法来解决这个问题。

谢谢

找到的解决方案: 感谢@Asherguru 的回答

我只需要稍微修改一下就可以达到预期的结果

以下工作

var transactions= (await _context.SalesTransactions.Include(x => x.Sender).ToListAsync())
            .GroupBy(w => new { w.CustomerId, w.Sender })
            .Select(x => new 
            {
                CustomerID= x.Key.CustomerId,
                 x.Key.Customer,
                Amount = x.Sum(w => w.Amount)
            }).ToList();

【问题讨论】:

    标签: c# linq linq-to-sql entity-framework-core left-join


    【解决方案1】:

    试试这个。

    await _context.SalesTransactions
            .GroupBy(w => w.CustomerId)
            .Select(x => new SalesTransactions() 
            {
                CustomerId = x.Key,
                Amount = x.Sum(w => w.Amount)
            }).ToListAsync();
    

    编辑 2

    await _context.SalesTransactions.Include(x => x.Customer).ToListAsync()
        .GroupBy(w => new { w.CustomerId, w.Customer })
        .Select(x => new SalesTransactions() 
        {
            CustomerId = x.Key.CustomerId,
            Customer = x.Key.Customer,
            Amount = x.Sum(w => w.Amount)
        }).ToListAsync();
    

    可以从 SalesTransaction 类中的 Customer.Name 中获取名称。

    【讨论】:

    • 谢谢你。非常有帮助。但是,我仍在努力通过加入名称字段来添加名称字段
    • 您需要设置Customer的ID和SalesTransactions的CustomerID之间的关系。然后您可以使用 Include(x => x.Customer) 并从 SalesTransaction.Customer.Name 获取名称
    • 已经有关系了。 SalesTransactions 表中有 CustimerId 字段
    • 然后,您可以使用 Include(x => x.Customer)。你已经可以从中得到名字了。无需加入名称,因为两个 CustomerId 都已链接。
    • 请更新答案,以便我接受。再次查看我的帖子,我添加了一个标有 SOLUTION FOUND 的部分,我在其中指出了需要对您的答案进行的修改。非常感谢。感谢您的努力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2020-05-19
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多