Order LINQ by "string" name
PROBLEM SOLVED!!!
The solution is Linq.Dynamic
You do it like this:
(from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year select c).OrderBy(order);
You have to download the System.Linq.Dynamic.dll and include it into your project.
Is there a way to order a linq query by the name of a field. like this:
from c in Context.AccountCharts
where c.Account_FK == account && c.Year_FK == year
orderby c["ColName"] select c;
Or
from c in Context开发者_StackOverflow中文版.AccountCharts
where c.Account_FK == account && c.Year_FK == year
orderby c.GetType().GetField("ColName") select c;
None of these two works but I hope you know of a way to do this.
I spend to many time to resolve this issue, but didn't find any better than:
var queryExpression;
if (c["ColName"]=="CreateDate")
queryExpression.OrderBy(x => x.CreateDate);
Hope this will work too though not tested
Context.AccountCharts.Where(c=>c.Account_FK == account && c.Year_FK == year).OrderBy(o=>o.order);
精彩评论