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), ] 其中第一个参数是正则匹配,如下 ...
随机推荐
- Kotlin入门(7)循环语句的操作
上一篇文章介绍了简单分支与多路分支的实现,控制语句除了这两种条件分支之外,还有对循环处理的控制,那么本文接下来继续阐述Kotlin如何对循环语句进行操作. Koltin处理循环语句依旧采纳了for和w ...
- Spark编译
Spark的运行版本使用mvn编译,已经集成在源码中.如果机器有外网或者配置了http代理,可以直接调用编译命令来进行编译. windows&Linux命令如下: ./build/mvn \ ...
- github版本控制相关
Git版本控制: 安装Github http://blog.csdn.net/huangyuan_xuan/article/details/49125597 Git本地版本控制 http://blog ...
- [20180612]删除bootstrap$记录无法启动.txt
[20180612]删除bootstrap$记录无法启动.txt --//前几天看链接http://www.xifenfei.com/2018/05/willfully-delete-bootstra ...
- sql server alwayson 可用性组 只读路由的设置
昨天晚上学习了[SQL Server 2012实施与管理实战指南]的第三章,于是今天想在前段时间建的那个alwayson 可用性组测试环境上也配置一下只读路由,尝试实现读写分离. 按照书中的方法,执行 ...
- Python3 下实现 腾讯人工智能API 调用
1.背景 a.鹅厂近期发布了自己的人工智能 api,包括身份证ocr.名片ocr.文本分析等一堆API,因为前期项目用到图形OCR,遂实现试用了一下,发现准确率还不错,放出来给大家共享一下. b.基于 ...
- 基于CoreText的基础排版引擎之不带图片的排版引擎
- (void)drawRect:(CGRect)rect { [super drawRect:rect]; //步骤一:得到当前绘制画布上下文,用于后续将内容绘制在画布上 CGContextRef ...
- (8)Python判断结构
- centos7下安装docker(13.1docker存储--data volume)
我们现在知道docker 有两种存储方式:storage driver和data volume stroage driver这种存储方式主要是存储那些无状态的数据,是镜像层和容器层组成的,而data ...
- request.servervariables参数
ServerVariables ServerVariables 集合检索预定的环境变量. 语法 Request.ServerVariables (server environment variable ...