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), ] 其中第一个参数是正则匹配,如下 ...
随机推荐
- “京东金融”主页效果 RecyclerView联动
先上效果图吧: 第一个想到的实现方式是上面使用horizontalScrollview,下面使用Viewpager,经过尝试之后发现二者API有限,不能达到理想效果.几经折腾,最后上下都使用了自定义的 ...
- Kotlin入门(3)基本变量类型的用法
上一篇文章介绍了Kotlin在App开发中的简单用法,包括操纵控件对象.设置控件监听器,以及弹出Toast提示等等.也许大家已经迫不及待想要了解更深入的App开发,可是由于Kotlin是一门全新的语言 ...
- mybatis学习系列四--mybatis generator逆向工程
采用命令行方式执行逆向工程 1.配置文件generatorConfig.xml 保存在目录:D:\E\workspace\eclipse\mybatis_generator <?xmlversi ...
- mybatis 初始
接下来带着大家建立一个mybatis的初级项目 首先我们利用idea利用maven建立一个空项目 然后输入名称什么的就会创建一个空的maven项目了 然后我们需要在项目总得pom.xml中进行配置信息 ...
- Python读取PE文件(exe/dll)中的时间戳
代码原文地址: https://www.snip2code.com/Snippet/144008/Read-the-PE-Timestamp-from-a-Windows-Exe https://gi ...
- Kibana中的Coordinate Map地图报索引错误的问题
今天做地图定位展示,展示的是ApacheWeb服务器的访问日志文件中的来源IP.但是中间出现了报错环节,说是索引不能匹配到geo_point类型,实在是不懂这是在说什么,后来在网站找了方法就解决了.主 ...
- Python-数学篇之计算方法的目录:
目录: 1.出本专题的初衷: 2.参考计算方法的书籍: 3.具体算法的实现: (一)出本专题的初衷: 在我们机械专业的大二上学期课程中,能与计算机沾上边的科目就数<计算方法>了.简化为&q ...
- tkinter学习系列之(六)Radiobutton控件
目录 目录 前言 (一)基本属性 (二)在Frame里布局: 目录 前言 Radiobutton单选框,在一组选框中,只能选中一个. (一)基本属性 (1)特有属性: value 按钮的值 varia ...
- SWFUpload多文件上传使用指南
SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大.以前在项目中用过几次,但它的配置参数太多了,用过后就忘记怎么用了,到以后要用时又得到官网上看它的文档,真是太烦了.所以 ...
- 使用sublime编写python、php代码前的一些配置
1.使用sublime编写python代码 打开sublime软件,Tools —> Build System —> New Build System,得到后缀名为“sublime-bui ...