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

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

 

 

 

转载于:https://www.cnblogs.com/coderinprague/p/Linq.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值