FCL研究-LINQ-System.Linq Enumerable

   .net 里面集合操作极为方便,尤其是实现了IEnumerable接口的集合,一直在使用,系统的研究一下集合的操作也是极好的。

类型操作符名称
投影操作符Select,SelectMany
限制操作符Where
排序操作符OrderBy,OrderByDescending,ThenBy,ThenByDescending,Reverse
联接操作符Join,GroupJoin
分组操作符GroupBy
串联操作符Concat
聚合操作符Aggregate,Average,Count,LongCount,Max,Min,Sum
集合操作符Distinct,Union,Intersect,Except
生成操作符Empty,Range,Repeat
转换操作符AsEnumerable,Cast,OfType,ToArray,ToDictionary,ToList,ToLookup
元素操作符DefaultIfEmpty,ElementAt,ElementAtOrDefault,First,Last,FirstOrDefault, LastOrDefault,Single,SingleOrDefault
相等操作符SequenceEqual
量词操作符All,Any,Contains
分割操作符Skip,SkipWhile,Take,TakeWhile

 

 投影操作符

  投影操作就是我们常用的Select和SelectMany,刚接触的话是很容易搞混的。

Select是把要遍历的集合IEnumerable<T>逐一遍历,每次返回一个T,合并之后直接返回一个IEnumerable<T>

SelectMany则把原有的集合IEnumerable<T>每个元素遍历一遍,每次返回一个IEnumerable<T>,把这些IEnumerable<T>的“T”合并之后整体返回一个IEnumerable<T>

     

看下MSN的图:地址

测试代码:

List<Bouquet> bouquets = new List<Bouquet>() {
        new Bouquet { Flowers = new List<string> { "sunflower", "daisy", "daffodil", "larkspur" }},
        new Bouquet{ Flowers = new List<string> { "tulip", "rose", "orchid" }},
        new Bouquet{ Flowers = new List<string> { "gladiolis", "lily", "snapdragon", "aster", "protea" }},
        new Bouquet{ Flowers = new List<string> { "larkspur", "lilac", "iris", "dahlia" }}
    };

    // *********** Select ***********            
    IEnumerable<List<string>> query1 = bouquets.Select(bq => bq.Flowers);

    // ********* SelectMany *********
    IEnumerable<string> query2 = bouquets.SelectMany(bq => bq.Flowers);

    Console.WriteLine("Results by using Select():");
    // Note the extra foreach loop here.
    foreach (IEnumerable<String> collection in query1)
        foreach (string item in collection)
            Console.WriteLine(item);

    Console.WriteLine("\nResults by using SelectMany():");
    foreach (string item in query2)
        Console.WriteLine(item);
View Code

 

转载于:https://www.cnblogs.com/BangQ/p/3981117.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值