Linq 和 Lambda 查询中按照多个值进行分组GroupBy
来源:互联网 发布:mac队员被杀 编辑:程序博客网 时间:2024/05/21 08:50
创建要查询的对象: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<Employee> empList = new List<Employee>();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<AddressBookPersonDto> bookPersons = (from a in addressClassPersons where a.AddressBookPerson != null select a.AddressBookPerson).ToList().MapTo<List<AddressBookPersonDto>>().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<AddressBookPersonDto> 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();
阅读全文
0 0
- Linq 和 Lambda 查询中按照多个值进行分组GroupBy
- Linq 中按照多个值进行分组(GroupBy)
- Linq 中按照多个值进行分组(GroupBy
- linq ,lamda 进行分组GroupBy查询
- Lambda和Linq三表连接查询加分组
- LINQ实现多次调用GroupBy进行分组统计
- 使用linq 中的GroupBy分组
- C#中linq 分组查询
- pandas中Groupby通过字典或Series进行分组
- python中Groupby使用(四)-通过函数进行分组
- c# Linq Lamda表达式使用 GroupBy 分组
- Sql groupby分组和stuff
- linq lambda 用法,查询
- 用linq对datatable进行分组查询并返回datatable
- C# 中使用Linq和Lambda表达式对List<T>进行排序
- C# 中使用LINQ和LAMBDA表达式对LIST进行多字段排序
- 在entity framework 中使用 LINQ 对表进行左关联查询且group by 分组查询的示例,并且按小时分组查询时间段
- datatable使用groupby进行分组统计
- CentOS7通过yum安装Mysql5.7+修改默认密码+远程登录
- 237. Delete Node in a Linked List的C++解法
- Scsi-target-utils Quickstart Guide
- Spring源码解析-PropertyEditor
- 使用EasyPermissions 来打造简单的android6.0动态权限
- Linq 和 Lambda 查询中按照多个值进行分组GroupBy
- vue.js浅度监听和深度监听
- 新浪微博OAuth认证和存储
- Android Studio显示行数
- Spring中property资源文件动态映射xml载入方法
- 第八章 VMware Workstation中Linux克隆步骤
- sonarqube安装使用
- 腾讯创业服务平台
- Java还要再学一遍基础(十一)WeakHashMap详解