Click here to Skip to main content
13,141,576 members (57,639 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 16 Jan 2011

Generating sequential indices using LINQ

, 17 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Many LINQ functions have a hidden parameter useful to generate or analyze sequential indices
Calculating index using index++.

int index = 0;
words = new ObservableCollection<LookupItem>(
    // trick number 1: converting string into char[]
    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()).
    // trick number 2: generating sequential numbers using LINQ
    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 }));


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Agroskin
Software Developer (Senior) Liquidnet
United States United States
Michael is a software developer who still remembers punch cards, computers with 4 Kbytes RAM, and 3270s. His personal computers were Apple IIe, Commodore, and PC XT (with the whole 640 Kbytes RAM and 2 floppy drives!!!). Wow, that was a powerhouse.

Fast forward 32 years through FORTRAN, PL-I, Algol, Pascal, Prolog, LISP, C, Basic, Clipper, Assembly, FoxPro, DHTML, JavaScript, C++, you name it, to C# 4.0.

Of course, real men use machine code to write software, but what a difference a few years make! No more mallocs and callocs, GC magically collects unused objects, dynamic objects magically call IUnknown::QueryInterface, Reflection magically gives you metadata and even generates help files, WPF magically binds stuff together...

Read some of Michael's articles here.

BindingHub (a WPF component and a design pattern) [^].

Notifying parent of changes to children's properties [^].

Point-In-Time database (coming soon)

Composite Menus and other ItemsControls (coming soon)

Adorners framework (coming soon)

Drag-n-drop data transfer framework (coming soon)

Converters and MarkupExtensions (coming soon)

Download complete WPF library [^].

You may also be interested in...


Comments and Discussions

GeneralShould we not be using ++i (prefix) to prevent the overhead ... Pin
jfriedman20-Jan-11 12:19
memberjfriedman20-Jan-11 12:19 
GeneralRe: Correct, but the point of my post was: we don't have to use ... Pin
Michael Agroskin20-Jan-11 12:35
memberMichael Agroskin20-Jan-11 12:35 
GeneralReason for my vote of 5 nice piece of code for indexing Pin
Pranay Rana17-Jan-11 0:41
memberPranay Rana17-Jan-11 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 17 Jan 2011
Article Copyright 2011 by Michael Agroskin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid