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子句正在更新内容.请稍后
随机推荐
- kendo-ui学习笔记(一)
1.top.jsp: <script src="<%=path%>/kendoui/js/jquery.min.js"></script> &l ...
- PHP 关于SQL注入的防范措施。
最近在使用框架的时候还是有点不安,不知道框架的设计者有没有考虑到SQL-Injection的问题,我在顶层需不需要做一些必要的过滤等等,由 此我特意的去StackOverflow看了下,真是获益良多, ...
- clip:rect矩形剪裁
clip:rect(top right bottom left);依据上-右-下-左的顺序提供自图片左上角为(0,0)坐标计算的四个偏移数值,其中任一数值都可用auto替换. 矩形剪裁 还需要绝对定位 ...
- 2015-2016最火的Android开源项目--github开源项目集锦(不看你就out了)
标签: Android开发开源项目最火Android项目github 2015-2016最火的Android开源项目 本文整理与集结了近期github上使用最广泛最火热与最流行的开源项目,想要充电与提 ...
- Directory 与 DirectoryInfo 的区别
Directory 与 DirectoryInfo 这两个能够实现大多数相同的功能.区别可分为两点:一.静态与实例调用前者必须被实例化后才能使用,而后者则只提供了静态的方法.如果多次使用某个对象一般使 ...
- php 画图片2
<?php // 使用php操作gd库做图 // 1. 创建一个画布资源 $im = imagecreatetruecolor(200, 50); // 2. 创建背景色 // 2.1 得到背景 ...
- kafka 订单应用需求
kafka的介绍就不说了,网上会找到一大堆. 为了公司做报表需要对卡券订单的销售情况做总结,所以每次下单的时候都要给卡券活动模块传递一次消息,并把订单的信息发送给活动,活动做相应的数据操作,因为数据量 ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
- php数组常见的几种遍历方法
1.foreach遍历 $arr[] = array('first'=>green,'second'=>'yellow','third'=>'blue'); foreach($arr ...
- Spring之实现任务调度
现实生活中,我们经常会制定一些"任务"在什么时间完成什么事情.同样在各种的企业中也会遇到这种任务调度的需求,比如商家或者网站的月报表 之类的要在每个月的最后一天统计各种数据,备份每 ...