按"字符串"名称命令LINQ

Ski*_*ner 6 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);
Run Code Online (Sandbox Code Playgroud)

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


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

from c in Context.AccountCharts 
    where c.Account_FK == account && c.Year_FK == year 
    orderby c["ColName"] select c;
Run Code Online (Sandbox Code Playgroud)

要么

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

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

And*_*ich 1

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

var queryExpression;
if (c["ColName"]=="CreateDate")
    queryExpression.OrderBy(x => x.CreateDate);
Run Code Online (Sandbox Code Playgroud)