Calculating
index
using
index++
.
int index = 0;
words = new ObservableCollection<LookupItem>(
from word in "Hello world from LINQ".Split(" ".ToCharArray())
select new LookupItem { Caption = word, Index = index++ }));
The same using built-in LINQ functionality.
words = new ObservableCollection<LookupItem>(
"Hello world from LINQ".Split(" ".ToCharArray()).
Select((word, index) => new LookupItem { Caption = word, Index = index }));
Other useful LINQ functions having second form with index are
TakeWhile
,
SkipWhile
and
Where
.
words = new ObservableCollection<LookupItem>(
"Hello world from LINQ".Split(" ".ToCharArray()).
Where((word, index) => index < 100).
Select((word, index) => new LookupItem { Caption = word, Index = index }));