按“字符串”名称命令LINQ

时间:2010-12-16 20:37:39

标签: linq string sql-order-by

问题已解决!!!

解决方案是Linq.Dynamic

你这样做:

(from c in Context.AccountCharts 
    where c.Account_FK == account && c.Year_FK == year select c).OrderBy(order);

您必须下载 System.Linq.Dynamic.dll 并将其包含在您的项目中。


有没有办法按字段名称订购linq查询。像这样:

from c in Context.AccountCharts 
    where c.Account_FK == account && c.Year_FK == year 
    orderby c["ColName"] select c;

或者

from c in Context.AccountCharts 
    where c.Account_FK == account && c.Year_FK == year 
    orderby c.GetType().GetField("ColName") select c;

这两个都不起作用,但我希望你知道这样做的方法。

2 个答案:

答案 0 :(得分:1)

我花了很多时间来解决这个问题,但没有找到比这更好的事情:

var queryExpression;
if (c["ColName"]=="CreateDate")
    queryExpression.OrderBy(x => x.CreateDate);

答案 1 :(得分:0)

希望这会在未经测试的情况下发挥作用

Context.AccountCharts.Where(c=>c.Account_FK == account && c.Year_FK == year).OrderBy(o=>o.order);