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子句的更多相关文章

  1. 【转】Linq Group by

    http://www.cnblogs.com/death029/archive/2011/07/23/2114877.html 1.简单形式: var q = from p in db.Product ...

  2. Linq之select子句

    在Linq中select子句用来指定查询结果的类型和表现形式.Linq查询要么以select子句结尾,要么以group子句结尾. List<UserBaseInfo> users = ne ...

  3. Linq:Group By用法

    1.简单形式: var q =from p in db.Products group p by p.CategoryID into g select g; 语句描述:使用Group By按Catego ...

  4. 选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

    选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中 T-SQL核心语句形式: SELECT     --指定要选择的列或行及其限定  [INTO ]      --INTO子句 ...

  5. SQL Server 基础 之 GROUP BY子句

    GROUP BY 子句用于聚合信息 先看个实例,没有使用 GROUP BY 子句 SELECT SalesOrderID,OrderQty FROM Sales.SalesOrderDetail WH ...

  6. group by子句的三点注意项

    1.在含有统计函数的select语句中,如果不使用group by子句,那么select子句中只允许出现统计函数,其他任何字段都不允许出现: 2.在含有统计函数的select语句中,如果使用了grou ...

  7. 含有GROUP BY子句的查询中如何显示COUNT()为0的成果(分享)

    在SQL Server数据库查询中,为了对查询成果进行对比.解析,我们经常会用到GROUP BY子句以及COUNT()函数来对查询成果进行分类.统计等.然则我们在应用的过程中往往会存在一些题目,本文我 ...

  8. T-SQL GROUP BY子句 分组查询

    SQL Server GROUP BY子句与SELECT语句协作使用,以将相同的数据分组. GROUP BY子句位于SELECT语句中的WHERE子句之后,位于ORDER BY子句之前. 语法 以下是 ...

  9. 1.5.3 GROUP BY子句

    1.5.3 GROUP BY子句正在更新内容.请稍后

随机推荐

  1. EXCEL里面的数字显示为文本 不用科学计数法显示

    1. 在输入这一串数字前加撇号“'”(英文状态下的单引号)即可.2. 先将这一列设置为“文本”格式,然后直接输入这一串数字即可.   已经输入好了数字,那估计你这些数字的后三位都已经全变成“0”了,用 ...

  2. python 笔记2:python语法基础

    python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...

  3. Unity中游戏的声音管理

    using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 用 ...

  4. 第二章 XHTML 基础

    元素与标签术语,HTML/XHTMLXHTML之间的联系区别在XHTML中,所有元素之间必须完成正确的嵌套,元素必须是闭合的,必须小写.必须有个跟元素HTML. 标题标<h1>语法:< ...

  5. Android ScrollView与ListView的冲突解决办法汇总

    1. public  void setListViewHeight(){ ListAdapter listadapter = lv.getAdapter(); if (listadapter == n ...

  6. 写简单游戏,学编程语言-python篇:大鱼吃小鱼

    很常见的游戏之一,实现原理并不复杂,并且参考了几个相关的代码.这边主要还是以学习编程语言和学习编程思路为重点记录一下吧.最近时间有点吃紧,只能匆忙记录一下.用pygame做的大鱼吃小鱼的游戏截图如下: ...

  7. angular 指令作用域 scope

    转载自:https://segmentfault.com/a/1190000002773689 下面我们就来详细分析一下指令的作用域. 在这之前希望你对AngularJS的Directive有一定的了 ...

  8. Win10(win7) 安装vs2015(2012)出现ASP.NET 4.0/4.5 尚未在 Web 服务器上注册 下载这个补丁安装就可以了

    url:https://www.microsoft.com/zh-cn/download/details.aspx?id=44907&a03ffa40-ca8b-4f73-0358-c191d ...

  9. Android 学习第14课,Android 布局

    布局分4种: 1. LinearLayout (线性布局) file:///H:/tool/01/Android/android-sdk-windows/docs/guide/topics/ui/la ...

  10. Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...