【问题标题】:LINQ GroupBy a name, but how to get Id of users?LINQ GroupBy 一个名称,但如何获取用户 ID?
【发布时间】: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;
                    }

}

【问题讨论】:

    标签: c# winforms linq group-by datagridview


    【解决方案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;
                        }
    
    }
    

    【讨论】:

    • 感谢您的回复。是的,它现在正在对您的代码进行少量修改。
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 2016-06-09
    • 2020-10-07
    • 1970-01-01
    • 2022-10-16
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多