即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Linq 与 Lambda 查询里按照多个值进行分组GroupBy

编程语言 Denghejing 60℃ 0评论
创建要查询的对象:
class Employee {
   public int ID { get;set; }
   public string FName { get; set; }
   public int Age { get; set; }
   public char Sex { get; set; }
}

如果对这个类的Age和Sex的连个字段进行分组,方法如下:

// 先造一些数据
List empList = new List();
empList.Add(new Employee() {
   ID = 1, FName = "John", Age = 23, Sex = 'M'
});
empList.Add(new Employee() {
   ID = 2, FName = "Mary", Age = 25, Sex = 'F'
});
empList.Add(new Employee() {
   ID = 3, FName = "Amber", Age = 23, Sex = 'M'
});
empList.Add(new Employee() {
   ID = 4, FName = "Kathy", Age = 25, Sex = 'M'
});
empList.Add(new Employee() {
   ID = 5, FName = "Lena", Age = 27, Sex = 'F'
});
empList.Add(new Employee() {
   ID = 6, FName = "Bill", Age = 28, Sex = 'M'
});
empList.Add(new Employee() {
   ID = 7, FName = "Celina", Age = 27, Sex = 'F'
});
empList.Add(new Employee() {
   ID = 8, FName = "John", Age = 28, Sex = 'M'
});


接下来的做法是:

// 实现多key分组的Linq扩展函数版本
var sums = empList
         .GroupBy(x => new { x.Age, x.Sex })
         .Select(group => new {
            Peo = group.Key, Count = group.Count()
         });
foreach (var employee in sums) {
   Console.WriteLine(employee.Count + ": " + employee.Peo);
}

// 实现多key分组的lambda版本
var sums2 = from emp in empList
            group emp by new { emp.Age, emp.Sex } into g
            select new { Peo = g.Key, Count = g.Count() };
foreach (var employee in sums) {
   Console.WriteLine(employee.Count + ": " + employee.Peo);
}


其他示例:
 【Lambda示例】
            List bookPersons = (from a in addressClassPersons
                                                      where a.AddressBookPerson != null
                                                      select a.AddressBookPerson).ToList().MapTo>().GroupBy(a => new
                                                        {
                                                            a.Id,
                                                            a.Name,
                                                            a.JobTitle,
                                                            a.FixedTelephone,
                                                            a.MobilePhone,
                                                            a.Email,
                                                            a.OrganizationId
                                                        })
                                                    .Select(g => new AddressBookPersonDto()
                                                    {
                                                        Id = g.Key.Id,
                                                        Name = g.Key.Name,
                                                        JobTitle = g.Key.JobTitle,
                                                        FixedTelephone = g.Key.FixedTelephone,
                                                        MobilePhone = g.Key.MobilePhone,
                                                        Email = g.Key.Email,
                                                        OrganizationId = g.Key.OrganizationId,
                                                        OrganizationName = g.Key?.OrganizationId > 0 ? _organizationManager.GetOrganization(g.Key.OrganizationId).DisplayName : String.Empty
                                                    }).ToList();
            
【Linq示例】
            List addressBookPersons = (from a in addressClassPersons
                                                             where a.AddressBookPerson != null
                                                             group a by new {
                                                                 a.AddressBookPerson.Id,
                                                                 a.AddressBookPerson.Name,
                                                                 a.AddressBookPerson.JobTitle,
                                                                 a.AddressBookPerson.FixedTelephone,
                                                                 a.AddressBookPerson.MobilePhone,
                                                                 a.AddressBookPerson.Email,
                                                                 a.AddressBookPerson.OrganizationId
                                                             } into g
                                                            select new AddressBookPersonDto()
                                                            {
                                                                Id = g.Key.Id,
                                                                Name = g.Key.Name,
                                                                JobTitle = g.Key.JobTitle,
                                                                FixedTelephone = g.Key.FixedTelephone,
                                                                MobilePhone = g.Key.MobilePhone,
                                                                Email = g.Key.Email,
                                                                OrganizationId = g.Key.OrganizationId,
                                                                OrganizationName = g.Key?.OrganizationId > 0 ? _organizationManager.GetOrganization(g.Key.OrganizationId).DisplayName : String.Empty
                                                            }).ToList();




转载请注明:CodingBlog » Linq 与 Lambda 查询里按照多个值进行分组GroupBy

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情