LINQ GroupBy 一个名称,但如何获取用户 ID?

Posted

技术标签:

【中文标题】LINQ GroupBy 一个名称,但如何获取用户 ID?【英文标题】:LINQ GroupBy a name, but how to get Id of users? 【发布时间】:2020-11-21 10:34:01 【问题描述】:

我想显示每个客户进行了多少次转账。 我想显示CustomerIdNameTotaly 的转账金额。 我的意思是我也想显示CustomerId,而不仅仅是NameAmount 转移。

CustomerId 外,一切正常:在我的 DataGridview 中它是空的。 我不知道该怎么做,如果能提供任何帮助,我将不胜感激。

这是我的代码:

using (Db db = new Db())

   var statistic = (from u in db.Transfers
                    join c in db.Customers on u.CustomerId equals c.CustomerId
                    where u.IsActive == true && u.Paid == true
                    group u by c.FirstName  into g
                    select new
                    
                      // here I get Headertext but not Id's value
                      Id = g.Select(x =>x.CustomerId),
                      Name = g.Key,
                      Totaly = g.Sum(x => x.Amount)
                    ).OrderByDescending(x => x.Totaly).ToList();
                    
                    if (statistic != null)
                    
                        dgvCustomerList.DataSource = statistic;
                    


【问题讨论】:

【参考方案1】:

正确的方法是按CustomerId 对其进行分组(因为CustomerId 是唯一的,您将在select 中获得不同的客户,然后您可以使用FirstOrDefault() 来获取FirstName 属性):

    using (Db db = new Db())

   var statistic = (from u in db.Transfers
                    join c in db.Customers on u.CustomerId equals c.CustomerId
                    where u.IsActive == true && u.Paid == true
                    group u by c.CustomerId  into g
                    select new
                    
                      Id = g.Key, // here I get Headertext but not Id's value
                      Name = g.FirstOrDefault().FirstName,
                      Totaly = g.Sum(x => x.Amount)
                    ).OrderByDescending(x => x.Totaly).ToList();
                    
                    if (statistic != null)
                    
                        dgvCustomerList.DataSource = statistic;
                    


【讨论】:

感谢您的回复。我已经开始使用 Arsalan 的小修改。甚至您的代码也可以正常工作。谢谢你们。 按多个属性对其进行分组并不是最优的。在这种情况下,更好的方法是按索引器属性分组 CustomerId 在索引器属性中,我的意思是数据库索引。生成的查询将更快、更优化【参考方案2】:

您可以同时按customerIdFirstName 进行分组

using (Db db = new Db())

   var statistic = (from u in db.Transfers
                    join c in db.Customers on u.CustomerId equals c.CustomerId
                    where u.IsActive == true && u.Paid == true
                    group u by new key c.FirstName,key c.CustumerId  into g
                    select new
                    
                      Id = g.Key.CustumerId,//g.Select(x =>x.CustomerId), // here I get Headertext but not Id's value
                      Name = g.Key.FirstName,
                      Totaly = g.Sum(x => x.Amount)
                    ).OrderByDescending(x => x.Totaly).ToList();
                    
                    if (statistic != null)
                    
                        dgvCustomerList.DataSource = statistic;
                    


【讨论】:

感谢您的回复。是的,它现在正在对您的代码进行少量修改。

以上是关于LINQ GroupBy 一个名称,但如何获取用户 ID?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 join 和 group by 在 C# Linq 中获取 Min?

在linq中使用GroupBy时,如何获取组名列表?

如何通过 LINQ 扩展获取 group 的分组值列表

Linq:GroupBy、Sum 和 Count

linq 组查询中的产品销售数量和名称

如何在 Dynamic Linq Core 中无任何分组