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语句究竟干嘛用,也许,明天的明天,我就会发现了。

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

  • 相关阅读:
    主流液晶显示器尺寸参数
    不能访问网络位置的解决方法(转)
    打开Word提示你正试图运行的函数包含有宏或需要宏语言支持的内容
    教你如何防“蹭网”
    ASA数据库瘦身(原创)
    多种解决:“Word无法启动转换器mswrd632.wpc”方法
    百兆线与千兆线网线制作方法
    linux常用命令
    DefaultIfEmpty
    实现手机发送验证码 进行验证
  • 原文地址:https://www.cnblogs.com/coderinprague/p/Linq.html
Copyright © 2011-2022 走看看