Linq To Objects Auto Increment Number

Posted by Nathan on Stack Overflow See other posts from Stack Overflow or by Nathan
Published on 2009-02-25T17:34:55Z Indexed on 2010/03/11 18:49 UTC
Read the original article Hit count: 732

Filed under:
|
|

This feels like a completely basic question, but, for the life of me, I can't seem to work out an elegant solution.

Basically, I am doing a Linq Query creating a new object from the query. In the new object, I want to generate a auto-incremented number to allow me to keep a selection order for later use (named Iter in my example).

Here is my current solution that does what I am needing:

    	Dim query2 = From x As DictionaryEntry In MasterCalendarInstance _
	 Order By x.Key _
	 Select New With {.CalendarId = x.Key, .Iter = 0}

	For i = 0 To query2.Count - 1
		query2(i).Iter = i
	Next

Is there a way to do this within the context of the linq query (so that I don't have to loop the collection after the query)? Thanks!

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about ASP.NET