LINQ where vs takewhile

后端 未结 6 1308
感情败类
感情败类 2021-01-30 07:46

I want to get a difference between TakeWhile & Where LINQ methods .I got the following data from MSDN .But It didn\'t make sense to me

Where

        
6条回答
  •  悲哀的现实
    2021-01-30 08:25

    TakeWhile stops when the condition is false, Where continues and find all elements matching the condition

    var intList = new int[] { 1, 2, 3, 4, 5, -1, -2 };
    Console.WriteLine("Where");
    foreach (var i in intList.Where(x => x <= 3))
        Console.WriteLine(i);
    Console.WriteLine("TakeWhile");
    foreach (var i in intList.TakeWhile(x => x <= 3))
        Console.WriteLine(i);
    

    Gives

    Where
    1
    2
    3
    -1
    -2
    TakeWhile
    1
    2
    3
    

提交回复
热议问题