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), ] 其中第一个参数是正则匹配,如下 ...
随机推荐
- loadrunner 场景设计-学习笔记之性能误区
场景设计-学习笔记之性能误区 by:授客 QQ:1033553122 场景假设: 每个事务仅包含一次请求,执行10000个并发用户数 性能误区: 每秒并发用户数=每秒向服务器提交请求数 详细解答: 每 ...
- Kotlin入门(18)利用单例对象获取时间
前面介绍了,使用扩展函数可以很方便地扩充数组Array的处理功能,例如交换两个数组元素.求数组的最大元素等等.那么除了数组之外,日期和时间的相关操作,也是很常见的,比如获取当前日期,获取当前时间.获取 ...
- couldn't locate lint-gradle-api-26.1.2.jar for flutter project
Could not find com.android.tools.lint:lint-gradle:26.1.2 当我尝试构建发行版本APK 时导致报这种错误,无法发行,针对自己的项目作出了相关修改, ...
- (网页)JS去掉字符串前后空格或去掉所有空格的用法(转)
转自脚本之家: 这篇文章主要介绍了JS去掉字符串前后空格或去掉所有空格的用法,需要的朋友可以参考下: 代码如下: function Trim(str) { return str.replace(/(^ ...
- Appium学习——安装appium Server
安装appium Server 下载地址:百度网盘的下载链接:https://pan.baidu.com/s/1pKMwdfX 下载后, AppiumForWindows.zip 进行解压,点击 ap ...
- 在c/c++中调用Java方法
JNI就是Java Native Interface, 即可以实现Java调用本地库, 也可以实现C/C++调用Java代码, 从而实现了两种语言的互通, 可以让我们更加灵活的使用. 通过使用JNI可 ...
- JMeter 脚本开发(五)
一.JMeter 元件运行顺序 执行顺序逻辑如下: 1.配置元件(如果存在) 2.前置处理器(如果存在) 3.定时器(如果存在) 4.取样器(如果存在) 5.后置处理器(如果存在且取样器的结果不为空) ...
- python第五十三天--进程,协程.select.异步I/O...
进程: #!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan import multiprocessing,threading,time ...
- Jquery获取当前页面中的复选框选中的内容
在使用$.post提交数据时,有一个数据是复选框获取数据,所以在当前页面获取到复选框选中的值并提交到后端卡住了一下,解决方法如下: 这两个input就是复选框的内容: str += "< ...
- Opengl正交矩阵 glOrthof 数学原理(转)
http://blog.sina.com.cn/s/blog_6084f588010192ug.html 在opengles1.1中设置正交矩阵只要一个函数调用就可以了:glOrthof,但是open ...