zoukankan      html  css  js  c++  java
  • 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(21, "limusha"));                        //通过构造函数构造新对象
                PersonList.Add(new Person(21, "guojing"));                        //通过构造函数构造新对象
                PersonList.Add(new Person(22, "wujunmin"));                    //通过构造函数构造新对象
                PersonList.Add(new Person(22, "lupan"));                        //通过构造函数构造新对象
                PersonList.Add(new Person(23, "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(21, "limusha"));                        //通过构造函数构造新对象
                PersonList.Add(new Person(21, "guojing"));                        //通过构造函数构造新对象
                PersonList.Add(new Person(22, "wujunmin"));                    //通过构造函数构造新对象
                PersonList.Add(new Person(22, "lupan"));                        //通过构造函数构造新对象
                PersonList.Add(new Person(23, "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了

     
  • 相关阅读:
    模仿微信右上角菜单栏
    改变checkbox的默认样式
    webpack中的静态资源处理
    如何解决Unsupported Architecture. Your executable contains unsupported architecture '[x86_64, i386]
    页面生命周期
    关于在使用Exchange2003系统时无法向sina,yahoo,hotmail等邮箱发送邮件问题的解决方法
    重置TCP/IP协议堆栈的经历
    网通电信双线路上网,网通的走网通线路,电信的走电信线路,内网通过NAT上网,双线路故障自动切换
    在OUTLOOK或OWA中查看邮件的SCL级别(转)
    常用的RBL服务器列表及介绍
  • 原文地址:https://www.cnblogs.com/cnshuji/p/5446383.html
Copyright © 2011-2022 走看看