zoukankan      html  css  js  c++  java
  • LINQ-Group子句、Into子句及orderby子句

    1. Group子句

      LINQ表达式必须以from子句开头,以select或Group子句结束,所以除了使用select子句也可以使用Group子句来返回元素分组后的结果。Group子句用来查询结果分组,并返回一对象序列。这些对象包含零个或多个与该组的key值匹配的项。

      注意:每个分组都不是单个元素,而是一个序列(也属于集合)。序列的元素类型为IGroup<TKey,Telement>(必须以Group子句结束的LINQ表达式,分组结果类型才为序列,序列的元素类型为IGroup<TKey,TElement>

     static void Main(string[] args)
            {
                //数据源
                List<Custom> my = new List<Custom>()
                {
                    new Custom() { name="上官杜伟",Age=30,Table=new List<string>() { "13166856811","17644015820"} },
                    new Custom() { name="上官王玥",Age=33,Table=new List<string>() { "12543356125","14588782536"} },
                    new Custom() { name="黄埔杜伟",Age=30,Table=new List<string>() { "13166856811","17644015820"} },
                    new Custom() { name="黄埔王玥",Age=33,Table=new List<string>() { "12543356125","14588782536"} }
                };
                //创建LINQ查询
                var query = from a in my
                            //安装名字的前两个字进行分组
                            group a by a.name.Substring(0,4);
    
                //输出语句
                foreach (var item in query)
                {
                    Console.WriteLine("分组键:{0}",item.Key);
    
                }
                Console.WriteLine("**********************************");
    
                Console.ReadKey();
            }

    2.Into子句

      into子句可以用来创建一个临时标识符,将group、join或select子句的结果存储到这个标识符中。

      static void Main(string[] args)
            {
                int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
                var q = from n in arr
                        where n > 1&& n<6
                        group n by n % 2 into g    //把N%2为分组  存进标识符g中,输出g
                        from sn in g
                        select sn;
    
                foreach (var item in q)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

    实例2

      

                //数据源
                string[] arr = {"杜伟","王玥","于海燕","高冬梅" };
    
                //创建查询语句
                var query = from a in arr
                            group a by a.Substring(0,2) into f
                            //根据条件输出分组后的结果
                            from b in f
                            select b;
                //输出结果
                foreach (var item in query)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

    3.OrderBy子句(中间无空格)

      orderby 子句可使返回的查询结果按升序或降序排序。升序关键字Ascending指定,而降序有关键字Descending指定。

    示例1.

      

    static void Main(string[] args)
            {
    
    
                //数据源
               int[] arr = {0,1,2,3,4,5,6,7,8,9};
    
                //创建查询语句
                var query = from a in arr
                            orderby a descending
                            select a;
                //输出结果
                foreach (var item in query)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }

    示例2

      

      static void Main(string[] args)
            {
    
    
                //数据源
               int[] arr = {0,1,2,3,4,5,6,7,8,9};
    
                //创建查询语句
                var query = from a in arr
                            orderby a%2 descending,a ascending
                            select a;
                //输出结果
                foreach (var item in query)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
  • 相关阅读:
    LeetCode换酒问题Swift
    LeetCode种花问题Swift
    LeetCode排序数组Swift
    retain, release, dealloc与retainCount的源码分析
    KVO后[obj class]与object_getClass(id obj)的结果竟会不一致?
    react的一些总结(与vue的比较学习)
    函数式编程---小记
    typescript学习--------进阶(2)
    学习typescript--------进阶
    学习typescript-----基础
  • 原文地址:https://www.cnblogs.com/xiaowie/p/9447985.html
Copyright © 2011-2022 走看看