Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a string variable, myFieldname.
How can I use its value as a alias in a linq expression?

string myFieldname = "theName";
var q = (from d in data select new { d.Id, myFieldname = d.Amount });


I want 'theName' be the alias not the 'myFieldname' itself.
Posted
Updated 8-Jul-11 20:25pm
v2

1 solution

No, you can't do that. To insert a variable in to a string and then execute it, requires the use of reflection. that is to say, d.Amount will be checked against the value in 'myfieldname', not the field named in myFieldname.
 
Share this answer
 
Comments
mehrdadc48 9-Jul-11 2:43am    
I meant now the result is:
Id myFieldname
--- -----------------
1 val1
2 val2

but I want to be:
Id theName
--- -----------------
1 val1
2 val2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900