LINQ where vs takewhile

后端 未结 6 1391
感情败类
感情败类 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:37

    Where can examine the whole sequence looking for matches.

    Enumerable.Range(1, 10).Where(x => x % 2 == 1)
    // 1, 3, 5, 7, 9
    

    TakeWhile stops looking when it encounters the first non-match.

    Enumerable.Range(1, 10).TakeWhile(x => x % 2 == 1)
    // 1
    

提交回复
热议问题