I want to set the column names of an output from LINQ dynamically. Like so:
summary Rows.Field<Type&g开发者_Go百科t;("Name")
I need to do this because I have to do the order by on three columns based on the a condition and each column have 2 different types like float
, int
and double
Does anyone have any suggestions on how this can be done?
LINQ to SQL deals with static types, though there are some tricks you can do. You can't change the type of the field on the fly, other than to use Convert.ToInt32, etc. to change the appropriate type to the value you need at the time you want to cast it.
Alternatively, you could try casting the value to each of the three types from the query:
from c in ctx
select new
{
IntVal = c.Value,
DoubleVal = Convert.ToDouble(c.Value),
.
.
}
This way, you'd have all three cast appropriately and can suck in the correct field. When working with the record, you can even use a wrapper around reflection to get something similarly to what you want. I don't know of Convert is LINQ supported, but you should be able to do something similarly.
HTH.
精彩评论