gpt4 book ai didi

.net - LINQ where 与 takewhile

转载 作者:行者123 更新时间:2023-12-03 04:54:12 25 4
gpt4 key购买 nike

我想了解 TakeWhile 之间的差异& Where LINQ方法。我从MSDN得到了以下数据。但这对我来说没有意义

Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) 

Filters a sequence of values based on a predicate.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)

Returns elements from a sequence as long as a specified condition is true.

欢迎所有意见。

最佳答案

TakeWhile当条件为假时停止,Where继续并查找所有符合条件的元素

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

关于.net - LINQ where 与 takewhile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5031726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com