How to name fields using Dynamic Linq?

I have a huge query that runs dynamically, but I want the select statement not to output column names, creating custom values. For example, if I execute a regular 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 nice accessors '.name' and '.price'

but if I use 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?

+4
source share
2 answers

ok, figured it out, here’s what you need:

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

You can also try this.

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

All Articles