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. 1、C#基础:变量、运算符、分支、循环、枚举、数组、方法 <转>

    转自:海盗船长 链接:http://www.cnblogs.com/baidawei/p/4701504.html#3241882 C#..Net以及IDE简介 一.什么是.Net? .Net指 .N ...

  2. Dapper学习笔记(3)-增、删、改、查

    一.建表 在数据库中建立如下三张表: CREATE TABLE [dbo].[T_User] ( , ) PRIMARY KEY NOT NULL, ) NOT NULL, ) NULL, ) NUL ...

  3. <textarea>使用的时候发现的两个问题的总结

    在练习表单的过程中,使用<textarea>时,遇到2个问题: 1.文本开始前有好多空格. 原来的代码是这样的: <textarea row="20" col=& ...

  4. Unity在PC上创建Excel文档

    NPOI下载连接:http://pan.baidu.com/s/1qWoITRI

  5. Android添加代码检查权限

    1,首先创建一个项目,然后创建一个类,hello.java public class hello { public static final String PERMISSION_SAY_HELLO = ...

  6. 使用TypeScript开发

    学习过一段时间CoffeeScript,然后再学习TypeScript,最后还是决定使用TypeScript开发. CofeeScript主要是给js添加一些语法糖,编写代码要快捷的多,少量的代码开发 ...

  7. 1.6jdk + eclipse + pydev搭建Python开发环境

    直接在1.6jdk的eclipse上用install new software的方法安装插件,会找不到安装好的插件.pydev官网还提供一种zip直接解压插件到eclipse文件夹下的dropins文 ...

  8. ASP.NET空网页生成默认代码注释

    当在Visual Studio下生成ASP.NET空网页时,默认生成代码: <%@ Page Language="C#" AutoEventWireup="true ...

  9. BackTrack5-r3 w3af无法更新问题解决

    wget http://pypi.python.org/packages/source/p/pybloomfiltermmap/pybloomfiltermmap-0.2.0.tar.gz --no- ...

  10. How can i use iptables save on centos 7?

    I installed CentOS 7 with minimal configuration (os + dev tools). I am trying to open 80 port for ht ...