Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用GroupBy和Sum进行Linq连接_C#_Linq_Linq To Sql_Entity Framework Core_Left Join - Fatal编程技术网

C# 使用GroupBy和Sum进行Linq连接

C# 使用GroupBy和Sum进行Linq连接,c#,linq,linq-to-sql,entity-framework-core,left-join,C#,Linq,Linq To Sql,Entity Framework Core,Left Join,我有一个交易表和一个客户表,如下所示: 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 Am

我有一个交易表和一个客户表,如下所示:

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)显示错误,表示“Sum在当前上下文中不存在”

我也不确定在查询语法中把grouping子句放在哪里以实现分组 由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();
试试这个

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

编辑2


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

谢谢。非常有用。然而,我仍然在努力通过加入name字段来添加name字段。您需要设置客户ID和SalesTransactions的CustomerID之间的关系。然后,您可以使用Include(x=>x.Customer)并从SalesTransaction.Customer.name获取名称已经存在关系。SalesTransactions表中有CustimerId字段,然后,您可以使用Include(x=>x.Customer)。你已经可以从中得到名字了。无需加入名称,因为两个CustomerId都已链接。请更新答案,以便我可以接受。再次查看我的帖子,我已经添加了一个标题为SOLUTION FOUND的部分,在那里我指出了对您的答案所需的修改。非常感谢你。我感谢你的努力
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();