.net LINQ where vs takewhile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5031726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ where vs takewhile
提问by Mohammed Thabet
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
我想在 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.
只要指定条件为真,就从序列中返回元素。
All opinions are welcomed.
欢迎所有意见。
回答by Albin Sunnanbo
TakeWhile stops when the condition is false, Where continues and find all elements matching the condition
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);
Gives
给
Where
1
2
3
-1
-2
TakeWhile
1
2
3
回答by Amy B
Wherecan examine the whole sequence looking for matches.
Where可以检查整个序列以寻找匹配项。
Enumerable.Range(1, 10).Where(x => x % 2 == 1)
// 1, 3, 5, 7, 9
TakeWhilestops looking when it encounters the first non-match.
TakeWhile当它遇到第一个不匹配时停止查找。
Enumerable.Range(1, 10).TakeWhile(x => x % 2 == 1)
// 1
回答by Jim Mischel
Say you have an array that contains [1, 3, 5, 7, 9, 0, 2, 4, 6, 8]. Now:
假设您有一个包含[1, 3, 5, 7, 9, 0, 2, 4, 6, 8]. 现在:
var whereTest = array.Where(i => i <= 5);will return [1, 3, 5, 0, 2, 4].
var whereTest = array.Where(i => i <= 5);会回来[1, 3, 5, 0, 2, 4]。
var whileTest = array.TakeWhile(i => i <= 5);will return [1, 3, 5].
var whileTest = array.TakeWhile(i => i <= 5);会回来[1, 3, 5]。
回答by naveen
MSDN says
MSDN 说
Returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.
只要指定条件为真,就从序列中返回元素,然后跳过其余元素。
Filters a sequence of values based on a predicate.
根据谓词过滤一系列值。
The difference is that Enumerable.TakeWhileskips the remaining elements from the first non-match whether they match the condition or not
区别在于从第一个不匹配的元素中Enumerable.TakeWhile跳过剩余的元素,无论它们是否匹配条件
回答by jmoreno
While the existing answers are correct, none of them point out whyyou'd want to use TakeWhile if the results would be the same: Performance. Suppose you have an ordered list with 2 billion items in it, and you want the ones that (probably 10 or 15 items) less than a given vallue. The Where clause will examine all 2 billion items, while the TakeWhile will stop as soon as it finds a value equal or greater than your supplied value
虽然现有的答案是正确的,但它们都没有指出如果结果相同,为什么要使用 TakeWhile:性能。假设您有一个包含 20 亿个项目的有序列表,并且您想要那些(可能是 10 或 15 个项目)小于给定值的项目。Where 子句将检查所有 20 亿个项目,而 TakeWhile 将在发现等于或大于您提供的值时立即停止
回答by StuartLC
The order of the sequence passed is absolutely critical with TakeWhile, which will terminate as soon as a predicate returns false, whereas Wherewill continue to evaluate the sequence beyond the first falsevalue.
传递的序列的顺序对于 绝对至关重要TakeWhile,一旦谓词返回false,Where它将立即终止,而将继续评估超出第一个false值的序列。
A common usage for TakeWhileis during the lazy evaluation of large, expensive, or even infinite enumerables where you may have additional knowledge about the ordering of the sequence.
for 的常见用法TakeWhile是在对大型、昂贵甚至无限可枚举的惰性求值期间,您可能对序列的排序有额外的了解。
e.g. Given the sequence:
例如给定序列:
IEnumerable<BigInteger> InfiniteSequence()
{
BigInteger sequence = 0;
while (true)
{
yield return sequence++;
}
}
A .Wherewill result in an infinite loop trying to evaluate part of the enumerable:
A.Where将导致尝试评估可枚举部分的无限循环:
var result = InfiniteSequence()
.Where(n => n < 100)
.Count();
Whereas a .TakeWhile, and armed with the knowledge that the enumerables is ascending, will allow the partial sequence to be evaluated:
而 a .TakeWhile,并且知道可枚举数是升序的,将允许评估部分序列:
var result = InfiniteSequence()
.TakeWhile(n => n < 100)
.Count();

