linq group by子句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication5
{
class Program
{
class Person
{
public int Age { set; get; }
public string Name { set; get; }
public Person(int Age,string Name)
{
this.Age = Age;
this.Name = Name;
}
}
static void Main(string[] args)
{
List<Person> PersonList = new List<Person>();
PersonList.Add(new Person(, "limusha")); //通过构造函数构造新对象
PersonList.Add(new Person(, "guojing")); //通过构造函数构造新对象
PersonList.Add(new Person(, "wujunmin")); //通过构造函数构造新对象
PersonList.Add(new Person(, "lupan")); //通过构造函数构造新对象
PersonList.Add(new Person(, "yuwen")); //通过构造函数构造新对象 //PersonList可以看做一张平面表,item是表中的(一)行记录
//下面的linq语句是在表PersonList中按照记录的Age字段进行分组
var query1 = from item in PersonList group item by item.Age;
//使用group子句进行分组
foreach (var element in query1)
{
Console.WriteLine(element.GetType().Name);//得到结果为三个Grouping类型
}
Console.ReadKey();
} }
}
在上边代码运行时
var query1 = from item in PersonList group item by item.Age;
foreach (var element in query1)
{
Console.WriteLine(element.GetType().Name);//得到结果为三个Grouping类型
}
得到结果为:
由此得到的是三个Grouping的数据类型,因此element的类型是Gouping,就是记录的分类这样一个类型
按照年龄分成三组,每组记录是Grouping类型 那么要得到具体的数据记录值怎么办呢?
这需要遍历每个分组,得到具体的每条记录(或者具体的字段值)才可以,我们接着对得到的分组数据进行遍历
foreach(var element in query1)//遍历分组数据
{
foreach(var item in element){//item是分组中的(一)记录,只有取到的是记录才能使用类中的属性名去访问
Console.WriteLine(
“姓名:”+item.Name+" "+"年龄"+item.Age.toString();
);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication5
{
class Program
{
class Person
{
public int Age { set; get; }
public string Name { set; get; }
public Person(int Age,string Name)
{
this.Age = Age;
this.Name = Name;
}
}
static void Main(string[] args)
{
List<Person> PersonList = new List<Person>();
PersonList.Add(new Person(, "limusha")); //通过构造函数构造新对象
PersonList.Add(new Person(, "guojing")); //通过构造函数构造新对象
PersonList.Add(new Person(, "wujunmin")); //通过构造函数构造新对象
PersonList.Add(new Person(, "lupan")); //通过构造函数构造新对象
PersonList.Add(new Person(, "yuwen")); //通过构造函数构造新对象 //PersonList可以看做一张平面表,item是表中的(一)行记录
//下面的linq语句是在表PersonList中按照记录的Age字段进行分组
var query1 = from item in PersonList group item by item.Age;
//使用group子句进行分组
foreach (var element in query1)
{
Console.WriteLine(element.GetType().Name);//得到结果为三个Grouping类型
foreach (var item in element) {
Console.WriteLine("年龄:"+item.Age+" 姓名: "+item.Name+" item类型为:"+item.GetType().Name);
}
}
Console.ReadKey();
} }
}
执行结果为
可以看到item的类型为Person了
linq group by子句的更多相关文章
- 【转】Linq Group by
http://www.cnblogs.com/death029/archive/2011/07/23/2114877.html 1.简单形式: var q = from p in db.Product ...
- Linq之select子句
在Linq中select子句用来指定查询结果的类型和表现形式.Linq查询要么以select子句结尾,要么以group子句结尾. List<UserBaseInfo> users = ne ...
- Linq:Group By用法
1.简单形式: var q =from p in db.Products group p by p.CategoryID into g select g; 语句描述:使用Group By按Catego ...
- 选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中
选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中 T-SQL核心语句形式: SELECT --指定要选择的列或行及其限定 [INTO ] --INTO子句 ...
- SQL Server 基础 之 GROUP BY子句
GROUP BY 子句用于聚合信息 先看个实例,没有使用 GROUP BY 子句 SELECT SalesOrderID,OrderQty FROM Sales.SalesOrderDetail WH ...
- group by子句的三点注意项
1.在含有统计函数的select语句中,如果不使用group by子句,那么select子句中只允许出现统计函数,其他任何字段都不允许出现: 2.在含有统计函数的select语句中,如果使用了grou ...
- 含有GROUP BY子句的查询中如何显示COUNT()为0的成果(分享)
在SQL Server数据库查询中,为了对查询成果进行对比.解析,我们经常会用到GROUP BY子句以及COUNT()函数来对查询成果进行分类.统计等.然则我们在应用的过程中往往会存在一些题目,本文我 ...
- T-SQL GROUP BY子句 分组查询
SQL Server GROUP BY子句与SELECT语句协作使用,以将相同的数据分组. GROUP BY子句位于SELECT语句中的WHERE子句之后,位于ORDER BY子句之前. 语法 以下是 ...
- 1.5.3 GROUP BY子句
1.5.3 GROUP BY子句正在更新内容.请稍后
随机推荐
- bak骗子公司
李波 身份证:310101197510313215 手机:13916407777 18621624812 13916821206住址:上海QQ:87766938 沪EE5781 奥迪Q7李寻欢77 s ...
- goldengate studio 12.2.1.2.6发布
主要特性: 1. 支持bigdata & teradata为目标端:
- python wordcloud 对电影《我不是潘金莲》制作词云
上个星期五(16/11/18)去看了冯小刚的最新电影<我不是潘金莲>,电影很长,有点黑色幽默.看完之后我就去知乎,豆瓣电影等看看大家对于这部电影的评价.果然这是一部很有争议的电影,无论是在 ...
- windows下Python shell代码自动补全
Unix下实现如题功能用下面的代码: import rlcompleter, readline readline.parse_and_bind('tab: complete') 但readline不能 ...
- luajava学习一
luajava环境的搭建就不写了,网上百度,google下就ok啦. 我是在android平台下运行的项目,好了先看代码 TextView tv=(TextView)findViewById(R.id ...
- Spring事务传播机制&隔离级别
一.Propagation (事务的传播属性) Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为.有以下选项可供使用:PROPAGATION_ ...
- Google play(Android market)账户申请
从09年到现在,Google play账户在国内申请一直都是个问题,根本的原因是国内不支持Google wallet(原Google Checkout) 在国内要获取一个Google play账户,常 ...
- Freemark基本语法知识(转)
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...
- linux命令:chgrp
1.命令介绍: chgrp用来改变文件或目录所属组的权限,要改变成的组必须在/etc/group文件存在才可以. 2.命令格式: chgrp [选项] 组 文件 3.命令参数: 必要参数: -c 当发 ...
- LayaAir引擎——(六)
LayaAir引擎——TiledMap地图图块属性获取和进行墙壁碰撞检测 需要的软件: TiledMap LayaAir IDE 1.0.2版本 所画的地图: pass层: floor层: pass层 ...