zoukankan      html  css  js  c++  java
  • LINQ 基本子句之一 (select/where/group/into)

    特别喜欢同事看到我写了一句小排序的时候说,他当然喜欢Linq了,虽然我只是baidu之,不知其然也不知其所以然。

    基本格式 var<变量> = from <项目> in <数据源> where <表达式> orderby <表达式> select <项目>

    1. from & select

            static void Main(string[] args)
            {
                string[] abc = { "a3", "af", "de" };
                   var result = from a in abc
                                     select a;
                foreach (var r in result)
                {
                    Console.WriteLine(r);
                }
                Console.ReadLine();
            }

    from为必须子句,select不必须。

    多个from子句

    static void Main(string[] args)
            {
                string[] abc = { "a3", "af", "de" };
                string[] de = { "af","de" };
                var result = from a in abc
                             from d in de
                             where a.Contains(d)
                             select a;
                foreach (var r in result)
                {
                    Console.WriteLine(r);
                }   
                 Console.ReadLine();
            }

    2.where


    where表示条件,也可有多个。或者省略where用&&连接

            static void Main(string[] args)
            {
                string[] abc = { "a3", "aff", "de" };
                string[] de = { "af","de" };
                var result = from a in abc
                             from d in de
                             where a.Contains(d)
                            && a.Length>2 //where a.Length>2
                             select a;
                foreach (var r in result)
                {
                    Console.WriteLine(r);
                }
                Console.ReadLine();
            }


    3.order by

       ascending/descending 默认 ascending

           static void Main(string[] args)
            {
                string[] abc = { "a3", "aff", "de" };
                string[] de = { "af","de" };
                var result = from a in abc
                             from d in de
                             where a.Contains(d)
                             orderby a.Length descending
                             select a;
                foreach (var r in result)
                {
                    Console.WriteLine(r);
                }
                Console.ReadLine();
            }

    4.group by

    在LINQ中,group对from语句进行分组,返回元素类型为IGrouping<TKey,TElement>的对象序列,因此每个返回需再次循环。

    如下:

         static void Main(string[] args)
            {  string[] xyz = { "a", "bc", "def" };
                var x = from o in xyz
                        group o by o.Length;
                foreach (var m in x)
                {
                    Console.Write(m.Key);
                    foreach (var p in m)
    
                        Console.WriteLine(p);
                }
                Console.ReadLine();
             }

    注意 group的语法为group x by y, 同时group在使用时,末尾没有select。

    5. into

    INTO 创建临时标识符用于保存查询的集合。

      static void Main(string[] args)
            {
                string[] xyz = { "a", "bc", "def" };
                var x = from o in xyz 
                        group o by o[0] into z
                        select z;
                
                foreach (var m in x)
                {
                    Console.Write(m.Key);
                    foreach (var p in m)
    
                        Console.WriteLine(p);
                }
                Console.ReadLine();
            }

    我知道这个例子略显神经,可是白痴的我还没发现into语句究竟干嘛用,也许,明天的明天,我就会发现了。

    因为——只要在路上,总会遇到庆典~

  • 相关阅读:
    mysql5.7 慢查底里失败的原因
    单体架构知识点及单体架构的缺陷
    分布式事务精华总结篇
    Kafka 消息丢失与消费精确一次性
    分布式柔性事务之最大努力通知事务详解
    Kafka面试题——20道Kafka知识点
    分布式柔性事务之事务消息详解
    奈学:数据湖有哪些缺点?
    奈学:数据湖和数据仓库的区别有哪些?
    了解概率知识,概率作为机器学习的底层逻辑
  • 原文地址:https://www.cnblogs.com/coderinprague/p/Linq.html
Copyright © 2011-2022 走看看