zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    可爱精灵宝贝 DP/爆搜
    那些年留的坑
    吃某种零食ing
    NOIP模拟测试13
    NOIP模拟测试12
    NOIP模拟测试11
    大佬 (数学)
    BZOJ3331 BZOJ2013 压力
    LOJ2586 APIO2018 选圆圈
    BZOJ3398 牡牛和牝牛
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6608091.html
Copyright © 2011-2022 走看看