区别在于:
Where会选取所有满足条件的集合;
TakeWhile会选取满足条件的集合,一旦遇到不满足条件的会中止搜索
例如:
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);
结果:
Where
1
2
3
-1
-2
TakeWhile
1
2
3
一目了然,了然于胸,胸有成竹了吧