Linq中的TakeWhile和SkipWhile

Linq中的TakeWhile和SkipWhile

Linq中的SkipWhile

1、含义

(1)、对数据源进行枚举,从第一个枚举得到的元素开始,调用客户端的predicate

(2)、如果返回true,则跳过该元素,继续进行枚举操作.

(3)、但是,如果一旦predicate返回为false,则该元素以后的所有元素,都不会再调用predicate,而全部枚举给客户端.

2、实现

  •  
  • C# 代码   复制
  • 
    public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
        if (source == null) throw Error.ArgumentNull("source");
        if (predicate == null) throw Error.ArgumentNull("predicate");
        return SkipWhileIterator<TSource>(source, predicate);
    }
    
    private static IEnumerable<TSource> SkipWhileIterator<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
        bool iteratorVariable0 = false;
        foreach (TSource iteratorVariable1 in source)
        {
            if (!iteratorVariable0 && !predicate(iteratorVariable1)) iteratorVariable0 = true;
            if (iteratorVariable0)
            {
                yield return iteratorVariable1;
            }
        }
    }
    
    		
  • 3、实例

  •  
  • C# 代码   复制
  • 
                int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
    
                IEnumerable<int> lowerGrades =
                    grades
                    .OrderByDescending(grade => grade)
                    .SkipWhile(grade => grade >= 80);
    
                Console.WriteLine("All grades below 80:");
                foreach (int grade in lowerGrades)
                {
                    Console.WriteLine(grade);
                }
    
                /*
                 This code produces the following output:
    
                 All grades below 80:
                 70
                 59
                 56
                */
    
    		
  • 二、Linq中的TakeWhile

    1、含义

    (1)、对数据源进行枚举,从第一个枚举得到的元素开始,调用客户端传入的predicate( c.Name == ""woodyN")

    (2)、如果这个predicate委托返回true的话,则将该元素作为Current元素返回给客户端,并且,继续进行相同的枚举,判断操作.

    (3)、但是,一旦predicate返回false的话,MoveNext()方法将会返回false,枚举就此打住,忽略剩下的所有元素.

    2、实例

  •  
  • C# 代码   复制
  • 
                string[] fruits = { "apple", "banana", "mango", "orange", 
                                      "passionfruit", "grape" };
    
                IEnumerable<string> query =
                    fruits.TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);
    
                foreach (string fruit in query)
                {
                    Console.WriteLine(fruit);
                }
    
                /*
                 This code produces the following output:
    
                 apple
                 banana
                 mango
                */
    
    		
  • 三、通过一个实例说明Linq中的TakeWhile和SkipWhile的区别

  •  
  • C# 代码   复制
  • 
    static List<Customer> customers = new List<Customer> {
    new Customer { CustomerID=1,Name="woody1"},
    new Customer { CustomerID=2,Name="woody2"},
    new Customer { CustomerID=3,Name="woody3"},
    new Customer { CustomerID=4,Name="woody1"}
    };
    
    var cs1 = customers.TakeWhile(c => c.Name == "woody1");
    var cs2 = customers.TakeWhile(c => c.Name == "woody2");
    var cs3 = customers.SkipWhile(c => c.Name == "woody1");
    var cs4 = customers.SkipWhile(c => c.Name == "woody2");
    
    		
  • cs1--cs4的结果

    cs1 : woody1(CustomerID=1)
    cs2 : 没有任何元素
    cs3 : woody2 , woody3 , woody1(CustomerID=4)
    cs4 : woody1(CustomerID=1),woody2,woody3,woody1(CustomerID=4)

    标签: