Please explain System.Linq.Enumerable.Where (predicate Func <T, int, bool>)
I can't understand the MSDN documentation for this overload of the Where method, which takes a predicate that takes two arguments, where int supposedly represents the index of the source element, whatever that means (I thought the enumerated was a sequence, and you couldn't see further than the next element, let alone indexing on it).
Can someone explain how to use this overload and, in particular, what is int in Func and how is it used?
The int parameter represents the index of the current element in the current iteration. Each time you call one of the LINQ extension methods, you are not theoretically guaranteed to receive items returned in the same order, but you know that they will all be returned once and therefore indexes can be assigned. (Well, you are guaranteed if you know that the request object is a List<T> or such, but not at all.)
Example:
var result1 = myEnumerable.Where((item, index) => index < 4); var result2 = myEnumerable.Take(4); // result1 and result2 are equivalent. You cannot index an IEnumerable<T> in the same way you can index an array, but you can use an index to filter the list in some way, or perhaps to index some data in another collection that will be used in state.
EDIT: as an example, skip any other element you can use:
var results = sequence.Where((item, idx) => idx % 2 == 0);