How can I use Dynamic Linq to name fields?

asp.net c# dynamic dynamic-linq linq

Question

I have a huge query that is being made dynamically, but I want the select statement to not output the column names, buut custom values. FOr example, if I am doing a normal Linq query, I can do something like this:

var v = from p in db.items select new { name = p.item_name, price = p.item_price };

which will give me the nice '.name' and '.price' accessors

but if I am using Dyanmic Linq, I can do this:

var v = db.items.Select("new (item_name,item_price)");

works fine, but

var v = db.items.Select("new (name=item_name,price=item_price)");

I get an error: "No property or field 'name' exists in type 'item'"

Can this be done?

1
4
2/11/2009 7:18:09 AM

Accepted Answer

ok, figured it out, this wis what was needed:

var v = db.items.Select("new (item_name as name,item_price as price)");
4
2/11/2009 8:05:09 AM

Popular Answer

you can also try this.

var v = db.items.Select("new(it[\"item_name\"]  as name,it[\"item_price\"] as price)"); 


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow