通过"string"命令LINQ姓名 [英] Order LINQ by "string" name

查看:58
本文介绍了通过"string"命令LINQ姓名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已解决!

解决方案是Linq.Dynamic

The solution is Linq.Dynamic

您这样做:

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

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

You have to download the System.Linq.Dynamic.dll and include it into your project.

是否有一种通过字段名称订购linq查询的方法.像这样:

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;

from c in Context.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);

这篇关于通过"string"命令LINQ姓名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆