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 [9]  | 
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
Wednesday, December 03, 2008 11:22:09 AM (Central European Standard Time, UTC+01:00)
As a newbie in linq - wondering about this "simple" problem - a sincere thank to you for this enlightening tip, and for making it publicly viewable.

Thank you from pm in italy.
pm
Monday, June 01, 2009 3:53:43 PM (Central European Standard Time, UTC+01:00)
Good evening. Over the years your bodies become walking autobiographies, telling friends and strangers alike of the minor and major stresses of your lives.
I am from Vatican and too bad know English, give true I wrote the following sentence: "The effect of anti allergic drugs on cytokine production from t cells should be cytokine imbalance is thought to be one of the causes for allergic diseases."

Thanks for the help ;), Carlton.
Tuesday, June 23, 2009 4:42:56 AM (Central European Standard Time, UTC+01:00)
You speak of i + + Here is the error!!!
It said: "The expression tree can not contain assignment operator"
why????
I hope you email me .. Thank you...
xiaolong
Thursday, July 23, 2009 7:47:11 PM (Central European Standard Time, UTC+01:00)
Doesnt work. I wish it did. Would make sense and make my life easier. You will get a The expression tree can not contain assignment operator error message.
You cant do the increment there in the LINQ sql.
Create separate class or function that does the counting and then call that instead. Google it, someone did it that way it works.
Ali
Monday, November 09, 2009 12:01:26 PM (Central European Standard Time, UTC+01:00)
var query = from author in DataContext.Authors
select author;
MyColumn.DataColumn = query.Index;
MyColumn.DataBind();
Monday, November 09, 2009 12:03:06 PM (Central European Standard Time, UTC+01:00)
woops: meant
MyColumn.DataColumn = query.ToList().Index; // should work
Tuesday, July 13, 2010 3:41:38 AM (Central European Standard Time, UTC+01:00)
Hello everybody, today really lucky
To introduce you these good online shops now!

Sale: GUCCI Bags, GUCCI Handbags, GUCCI Shoes, GUCCI Clothes, GUCCI Glasses, etc.
http://www.gucciboss.com/

2010 NFL, NFL Jerseys, NFL Jersey, NHL Jerseys, MLB Jerseys, NBA Jerseys, wholesale NFL Jerseys
http://www.2010-nfl.com/


Vibram,Five Fingers,fivefingers,Five Fingers Kso,Five Fingers Classic,Five Fingers Sprint,FiveFingers shoes,Vibram Outlet Sale
http://www.topfivefingers.com/

Christian Louboutin,Christian Louboutin shoes,Christian Louboutin sale,ChristianLouboutin Boots,Christian Louboutin Pumps,Christian Louboutin Sandals
http://www.superchristianlouboutin.com
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