LINQ 学习路程 -- 查询操作 Skip SkipWhile Take TakeWhile

MethodDescription
Skip 跳过序列中指定数量元素,然后返回剩余序列
SkipWhile 只要满足条件,就跳过序列中的元素,然后返回剩余函数
Take 从序列的开头返回指定数量的连续元素
TakeWhile 只要满足条件,就返回元素
IList<string> strList = new List<string>(){ "One", "Two", "Three", "Four", "Five" };

var newList = strList.Skip(2);

foreach(var str in newList)
    Console.WriteLine(str);

 

 

IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };

var resultList = strList.SkipWhile(s => s.Length < 4);

foreach(string str in resultList)
        Console.WriteLine(str);

 

SkipWhile

找到第一个与给定条件匹配的元素,然后跳过该元素获取剩余元素

从集合第一个元素判断,如果满足条件,则继续判断下一个元素,知道不满足条件后,跳过满足条件的元素返回剩下的序列

IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };

var resultList = strList.SkipWhile(s => s.Length < 4);

foreach(string str in resultList)
        Console.WriteLine(str);

 

 

 

 

Take

 

 

IList<string> strList = new List<string>(){ "One", "Two", "Three", "Four", "Five" };

var newList = strList.Take(2);

foreach(var str in newList)
    Console.WriteLine(str);

 

IList<string> strList = new List<string>() { 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Hundred"  };

var result = strList.TakeWhile(s => s.Length > 4);

foreach(string str in result)
        Console.WriteLine(str);

 

 

 

 

IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };

var resultList = strList.TakeWhile((s, i) => s.Length > i);

foreach(string str in resultList)
        Console.WriteLine(str);

 

posted @ 2017-03-24 00:04  蓝平凡  阅读(1179)  评论(0编辑  收藏  举报