tags:

views:

101

answers:

3
+1  Q: 

Indexing in Linq

Why do we use index in "where clause"? Is it an auto generated number ans starts from zero? Simple example would be really helpful.

var query =... Where((p,index)..)
+1  A: 

The index should refer to the index of the current item in the collection (the zero based iteration).

There is a simple example on this page.

Quintin Robinson
Thank you Robin :)
+3  A: 

Yes, it is an auto-generated number that starts from zero.

Use it whenever you need access to the index in your query.

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var evenLetters = alphabet.Where((p, index) => (index % 2) == 1);
var oddLetters = alphabet.Where((p, index) => (index % 2) == 0);
LukeH
Thank you Luke for the example
+1  A: 
var oddElements = query.Where((p, index) => index % 2 == 1);
Yuriy Faktorovich
Thank you Yuriy :)