C# 泛型分组和Linq分组的异同
没什么好说的,因为用的到,所以作个记录,
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleMe
{
class Program
{
static List<Person> persons1 = new List<Person>();
static void Main(string[] args)
{
persons1.Add(new Person("张三", "男", , ));
persons1.Add(new Person("王成", "男", , ));
persons1.Add(new Person("李丽", "女", , ));
persons1.Add(new Person("何英", "女", , ));
persons1.Add(new Person("何英", "女", , )); Console.WriteLine("泛型分组如下:"); var ls = persons1.GroupBy(a => a.Sex).Select(g => (new { sex = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));
foreach (var item in ls)
{
Console.WriteLine(item.sex + " " + item.count + " " + item.ageC + " " + item.moneyC); } ////////////////////////////////////////////////////////////////////////////////////////////////
Console.WriteLine("");
Console.WriteLine("LIQN分组如下:"); var ls2 = from ps in persons1
group ps by ps.Sex
into g
select new { sex = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };
foreach (var item in ls2)
{
Console.WriteLine(item.sex + " " + item.count + " " + item.ageC + " " + item.moneyC); } Console.Read();
}
} public class Person
{
public string Name { get; set; }
public int Age { get; private set; }
public string Sex { get; set; }
public int Money { get; set; } public Person(string name, string sex, int age, int money)
{
Name = name;
Age = age;
Sex = sex;
Money = money;
}
}
}
执行截图:

后续...
敬请期待...
如果是多列/多个属性参与分组应当如何呢?
代码如下:
namespace Test2
{
class Program
{
static List<Person> persons1 = new List<Person>();
static void Main(string[] args)
{
persons1.Add(new Person("张三", "男", , , ));
persons1.Add(new Person("王成", "男", , , ));
persons1.Add(new Person("李丽", "女", , , ));
persons1.Add(new Person("何英", "女", , , ));
persons1.Add(new Person("王红", "女", , , )); Console.WriteLine("泛型多属性/多列分组如下:"); var ls = persons1.GroupBy(A => new { A.Sex, A.SexId }).Select(g => (new { sex = g.Key.Sex, sexId = g.Key.SexId, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));
foreach (var item in ls)
{
Console.WriteLine(item.sex + " " + item.count + " " + item.ageC + " " + item.moneyC); } ////////////////////////////////////////////////////////////////////////////////////////////////
Console.WriteLine("");
Console.WriteLine("LIQN多属性/多列分组如下:"); var ls2 = from ps in persons1
group ps by new { ps.Sex,ps.SexId}
into g
select new { sex = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };
foreach (var item in ls2)
{
Console.WriteLine(item.sex + " " + item.count + " " + item.ageC + " " + item.moneyC); } Console.Read();
}
} public class Person
{
public string Name { get; set; }
public int Age { get; private set; }
public string Sex { get; set; }
public int SexId { get; set; }
public int Money { get; set; } public Person(string name, string sex, int age, int money, int sexId)
{
Name = name;
Age = age;
Sex = sex;
Money = money;
SexId = sexId;
}
} public class PersonGroup
{
public string Sex { get; set; }
public int SexId { get; set; }
public List<NewPerson> PersonLs { get; set; }
} public class NewPerson
{
public string Name { get; set; }
public int Age { get; private set; }
public int Money { get; set; }
}
}
未完,持续...
敬请期待...
本次补充AutoMapper的使用,我们将分组的数据映射到类,代码如下:
程序集下载地址:http://files.cnblogs.com/files/chenwolong/AutoMapper.rar
也可通过NuGet获取
需要程序集AutoMapper.dll 引入命名空间:using AutoMapper;
namespace ConsoleMe
{
class Program
{
static List<Person> persons1 = new List<Person>();
static void Main(string[] args)
{
persons1.Add(new Person("张三", "男", , , ));
persons1.Add(new Person("王成", "男", , , ));
persons1.Add(new Person("李丽", "女", , , ));
persons1.Add(new Person("何英", "女", , , ));
persons1.Add(new Person("王红", "女", , , )); Console.WriteLine("泛型多属性/多列分组如下:"); var ls = persons1.GroupBy(A => new { A.Sex, A.SexId }).Select(g => (new { sex = g.Key.Sex, sexId = g.Key.SexId, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));
foreach (var item in ls)
{
Console.WriteLine(item.sex + " " + item.count + " " + item.ageC + " " + item.moneyC); } ////////////////////////////////////////////////////////////////////////////////////////////////
Console.WriteLine("");
Console.WriteLine("LIQN多属性/多列分组如下:"); var ls2 = from ps in persons1
select new { sex = ps.Sex, Name = ps.Name, Age = ps.Age, Money = ps.Money, SexId = ps.SexId };
var Kar = ls2.GroupBy(g => new { g.sex, g.SexId }).Select(S => new { S.Key.sex, S.Key.SexId, PersonList = S });
//使用AutoMap 将 kar 映射到对应的类
List<PersonGroup> AllList = Mapper.DynamicMap<List<PersonGroup>>(Kar);
foreach (var item in AllList)
{
Console.WriteLine("性别为" + item.Sex + "的童鞋有:");
int index = AllList.IndexOf(item);
foreach (var childItem in AllList[index].PersonList)
{
Console.WriteLine("姓名为:" + childItem.Name + ",年龄为:" + childItem.Age + ",金钱为:" + childItem.Money + "。");
}
} var data = Kar.ToList(); Console.Read();
}
} public class Person
{
public string Name { get; set; }
public int Age { get; private set; }
public string Sex { get; set; }
public int SexId { get; set; }
public int Money { get; set; } public Person(string name, string sex, int age, int money, int sexId)
{
Name = name;
Age = age;
Sex = sex;
Money = money;
SexId = sexId;
}
} public class PersonGroup
{
public string Sex { get; set; }
public int SexId { get; set; }
public List<NewPerson> PersonList { get; set; }
} public class NewPerson
{
public string Name { get; set; }
public int Age { get; private set; }
public int Money { get; set; }
}
}
以上代码适用于:一对多的类映射

@陈卧龙的博客
C# 泛型分组和Linq分组的异同的更多相关文章
- Linq分组功能
Linq在集合操作上很方便,很多语法都借鉴自sql,但linq的分组却与sql有一定的区别,故整理发布如下. 1. Linq分组 分组后以Key属性访问分组键值. 每一组为一个IEnumberAbl ...
- Linq分组,linq方法分组
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: 01.public class ...
- Linq分组操作之GroupBy,GroupJoin扩展方法源码分析
Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...
- c# linq 分组groupby
转载: https://www.cnblogs.com/cncc/p/9846390.html 一.先准备要使用的类: 1.Person类: class Person { public string ...
- Django之无名分组,有名分组
在Django 2.0版本之前,在urls,py文件中,用url设定视图函数 urlpatterns = [ url(r'login/',views.login), ] 其中第一个参数是正则匹配,如下 ...
- 按照grouip分组,之后分组调用生成正式凭证 的接口
按照grouip分组,之后分组调用生成正式凭证 的接口 Map<String, List<OperatingLogVO>> resultMap = new HashMap< ...
- Web框架之Django_03 路由层了解(路有层 无名分组、有名分组、反向解析、路由分发 视图层 JsonResponse,FBV、CBV、文件上传)
摘要: 路由层 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态网页.虚拟环境 视图层 JsonResponse FBV 与 CBV(function base views与class bas ...
- sql-实现select取行号、分组后在分组内排序、每个分组中的前n条数据
表结构设计: 实现select取行号 sql局部变量的2种方式 set @name='cm3333f'; select @id:=1; 区别:set 可以用=号赋值,而select 不行,必须使用:= ...
- django基础之有名分组和无名分组
在Django 2.0版本之前,在urls,py文件中,用url设定视图函数 urlpatterns = [ url(r'login/',views.login), ] 其中第一个参数是正则匹配,如下 ...
随机推荐
- Linux 操作系统主机名变成bogon怎么解决?
主机名变成bogon怎么解决? by:授客 QQ:1033553122 一:使用hostname命令 [laiyu@localhost ~]$ hostname localhost.localdo ...
- Android Context类
一.Context类的使用场景 ①.创建Application对象时,而且整个app公用一个Application对象 ②.创建Service对象时 ③.创建Activity对象时 二.应用程序共有几 ...
- Kotlin入门(3)基本变量类型的用法
上一篇文章介绍了Kotlin在App开发中的简单用法,包括操纵控件对象.设置控件监听器,以及弹出Toast提示等等.也许大家已经迫不及待想要了解更深入的App开发,可是由于Kotlin是一门全新的语言 ...
- JMeter—配置元件(七)
参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第二节配置元件JMeter配置元件可以用来初始化默认值和变量,以便后续采样器使用.将在其作用域的初始化阶段处理. ...
- 安装SQL 2005 出现警告 ,32位ASP.NET已经注册,需要注册64位
将64位.net注册到iis上 cscript C:\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 ...
- C#检测本机是否联网
public class Net { [DllImport("wininet")] private extern static bool InternetGetConnectedS ...
- 第七章 Hyper-V 2012 R2 授权管理
当企业或组织的规模越来越大时,维护某一项单独的应用可能会由特定的运维人员进行管理.考虑到安全风险的问题,一般特定的运维人员不会拥有域管理员权限.自 Windows Server 2012 开始,操作系 ...
- git 创建本地仓库、远程仓库,上传项目
1.在本地想创建git仓库的地方创建本地仓库 首先右键打开 Git Bash Here,如果没有,请先安装git,下载地址:https://git-scm.com/downloads git init ...
- Linux 小知识翻译 - 「端口和端口号」
这次说说「端口」和「端口号」. 平时经常会听人说「打开了80号端口」,为了安全「不要打开多余的端口」等等.那么,这里的端口或者端口号是什么呢? 首先,「端口」是TCP或者UDP上使用的概念,经常被比喻 ...
- Deepin中设置文件或文件夹权限
Deepin中设置文件或文件夹权限 -R 递归进行某项操作,不论是删除文件夹或者修改文件夹下所有文件权限 权限更改,777相当于完全控制权限: 更改一个文件夹或文件的权限:chmod 777 文件 ...