本文介绍了LINQ哪里VS takewhile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得takewhile和放大器之间的差;在那里。我从MSDN得到了以下数据。但它的LINQ方法没有意义的,我

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<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) 

筛基于一个predicate值序列。

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时停止条件为假,如果继续,找到匹配条件的所有元素

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);

给出

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

这篇关于LINQ哪里VS takewhile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 01:13