没什么好说的,因为用的到,所以作个记录,

代码如下:

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分组的异同的更多相关文章

  1. Linq分组功能

    Linq在集合操作上很方便,很多语法都借鉴自sql,但linq的分组却与sql有一定的区别,故整理发布如下. 1.  Linq分组 分组后以Key属性访问分组键值. 每一组为一个IEnumberAbl ...

  2. Linq分组,linq方法分组

    Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: 01.public class ...

  3. Linq分组操作之GroupBy,GroupJoin扩展方法源码分析

    Linq分组操作之GroupBy,GroupJoin扩展方法源码分析 一. GroupBy 解释: 根据指定的键选择器函数对序列中的元素进行分组,并且从每个组及其键中创建结果值. 查询表达式: var ...

  4. c# linq 分组groupby

    转载: https://www.cnblogs.com/cncc/p/9846390.html 一.先准备要使用的类: 1.Person类: class Person { public string ...

  5. Django之无名分组,有名分组

    在Django 2.0版本之前,在urls,py文件中,用url设定视图函数 urlpatterns = [ url(r'login/',views.login), ] 其中第一个参数是正则匹配,如下 ...

  6. 按照grouip分组,之后分组调用生成正式凭证 的接口

    按照grouip分组,之后分组调用生成正式凭证 的接口 Map<String, List<OperatingLogVO>> resultMap = new HashMap< ...

  7. Web框架之Django_03 路由层了解(路有层 无名分组、有名分组、反向解析、路由分发 视图层 JsonResponse,FBV、CBV、文件上传)

    摘要: 路由层 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态网页.虚拟环境 视图层 JsonResponse FBV 与 CBV(function base views与class bas ...

  8. sql-实现select取行号、分组后在分组内排序、每个分组中的前n条数据

    表结构设计: 实现select取行号 sql局部变量的2种方式 set @name='cm3333f'; select @id:=1; 区别:set 可以用=号赋值,而select 不行,必须使用:= ...

  9. django基础之有名分组和无名分组

    在Django 2.0版本之前,在urls,py文件中,用url设定视图函数 urlpatterns = [ url(r'login/',views.login), ] 其中第一个参数是正则匹配,如下 ...

随机推荐

  1. loadrunner 脚本开发-字符串编码转换

    字符串编码转换 by:授客 QQ:1033553122   相关函数 lr_convert_string_encoding函数 功能:字符串编码转换 原型: int lr_convert_string ...

  2. C# winform三种定时方法

    1. 直接用winform 的 timers 拖控件进去 代码 public partial class Form1 : Form     {         public Form1()       ...

  3. scrapy系列(四)——CrawlSpider解析

    CrawlSpider也继承自Spider,所以具备它的所有特性,这些特性上章已经讲过了,就再在赘述了,这章就讲点它本身所独有的. 参与过网站后台开发的应该会知道,网站的url都是有一定规则的.像dj ...

  4. python第五十七天------补上笔记

    direct_client:广播接收 #!/usr/bin/env python #_*_coding:utf-8_*_ import pika,time,sys connection = pika. ...

  5. Hive-1.2.1_03_DDL操作

    Hive官方文档:Home-UserDocumentation Hive DDL官方文档:LanguageManual DDL 参考文章:Hive 用户指南 注意:各个语句的版本时间,有的是在 hiv ...

  6. DFS普及组常用模板简单整理

    一些普及组会用到的DFS模板,其他的DFS我感觉普及组不会用到所以暂且搁着,等之后有时间了再细写w (至于我为什么最近不写TG相关只写最基础的PJ的内容,请戳这里了解) dfs各种模板big集合 1. ...

  7. vuejs_01项目启动

    知识点 .npm 相关命令 npm list -g --depth= 查看全局安装了哪些依赖 项目启动 npm install vue-cli -g 安装vue脚手架 vue init webpack ...

  8. Jquery 中 ajaxSubmit使用讲解

    最近在使用ajaxForm,随便把使用方法记下下来,以便以后回顾. 1 ,引入依赖脚本 <script type="text/javascript" src="/j ...

  9. luogu P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...

  10. python中的轻量级定时任务调度库:schedule

    提到定时任务调度的时候,相信很多人会想到芹菜celery,要么就写个脚本塞到crontab中.不过,一个小的定时脚本,要用celery的话太“重”了.所以,我找到了一个轻量级的定时任务调度的库:sch ...