开发者

What does "(n, index) => n >= index" mean in LINQ?

I'm learning LINQ and came across the following code in one of the books:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstBigNumbers = numbers.TakeWhile((n,index)=>n>=index);

What is (n,index)?

I came across () =>, which means a lambda expression that takes no arguments.

Now the question is: How m开发者_JAVA百科any arguments a lambda expression can take? How to decide the number of arguments to write in my code?


(n, index) are the parameters of a lambda expression. In that case they represent an item of the collection and its index in the collection. This can be inferred from the signature of the TakeWhile method:

public static IEnumerable<TSource> TakeWhile<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, int, bool> predicate
)

The lambda expression (n,index) => n >= index represents a predicate: TakeWhile will continue to yield items while this predicate is satisfied.


In simple terms, it means return all the numbers which are greater than (or equal to) their position in the array, until you find one which doesn't fit. i.e. It just returns the first two (5 and 4).

EDIT: Just to add, if you wanted to get all such numbers (i.e. not to stop the first time you encountered one which didn't fit), you could change the TakeWhile to a Where.

e.g.

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var allBigNumbers = numbers.Where((n, index) => n >= index);

returns 5,4,3,9,8,6,7 (remember it's a zero-based array, hence 3 being returned). And I renamed it to allBigNumbers so it makes sense!


(n,index)

is the parameter list for the lambda expression (in place function definition).

n => index

would be the function body (expression body to be precise). In this case, it it wrong, because it would return another lambda of which the parameter type cannot be inferred, and which is also not castable to 'bool'. TakeWhile accepts a Func<bool> so, it won't compile.

This makes more sense:

numbers.TakeWhile((n,index)=> n>=index)


The code you have currently doesn't make much sense, since the lambda expression (n,index)=>(n=>index) (brackets added for clarity) returns another lambda expression going from n (value in the list) to (index) position in the list. As has been said you probably mean (n,index) => (n >= index)


If you look at the documentation for TakeWhile you will see that the argument you are looking at is a predicate with the following signature:

 Func<TSource, int, bool> predicate

The Func part indicates that you can supply a delegate that takes an object of type TSource and an integer, which is the index of the item in the list, and return a bool. This allows you to filter items in the list based on their index and some condition.

It looks like you code has a typo, and should read:

TakeWhile((n,index) => n > index)

This will return the integers up until the first one which exceeds its index within the given list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜