Saturday, April 05, 2008

Today I needed to add an special field to my LINQ query. A kind of Row Number column. Actually it was not a row number nor was it a column since LINQ deals with objects, but since it was used for presenting data in a tabular way, I see no other name that fits the description.

So, I needed a data displayed in a following format:

Lp. Name
1 Murray N. Rothbard
2 Hans-Hermann Hoppe
3 Ludwig von Mises

First column had to be generated somehow. Using LINQ. I made few quick searches on google, but whith no success, so I came out with my own solution:

int i = 1;
return from author in DataContext.Authors
       select new Author() { Lp = i++, Name = author.Name };

So very simple. Isn't it?

Friday, April 04, 2008 11:15:34 PM (Central European Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [2]  | 
Friday, May 23, 2008 9:06:17 PM (Central European Standard Time, UTC+01:00)
How would you do this in vb.net?
John
Thursday, August 21, 2008 12:56:40 AM (Central European Standard Time, UTC+01:00)
Just wanted to thank you for this little gem.
Helps to illustrate the power of LINQ
and educate me on how to leverage it.

Many Thanks, LA Guy in LA, CA.
LA Guy
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview