我希望能够使用lambda表达式获得OrderBy
查询,以便使用TOP(n)关键字(大性能提升)获得SQL查询。在linq查询中设置动态排序名称字段
我可以,如果我specifiy做到这一点...
PaginatedList = query.OrderBy(x => x.QuoteID).Skip(() => skipValue).Take(() => pageSize)
但因为我想要的排序依据字段设置为通过名字的UI选择我想要做这样的事情的动态:
var propertyInfo = typeof(Data.Quote).GetProperty(sortName);
Expression<Func<Data.Quote, object>> orderField = x => propertyInfo.GetValue(x, null);
PaginatedList = query.OrderBy(orderField).Skip(() => skipValue).Take(() => pageSize)
这给我的错误:
"LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression."
这个我试过那是类型的不
var propertyInfo = typeof(Data.Quote).GetProperty(sortName);
Func<Data.Quote, object> orderField = x => propertyInfo.GetValue(x, null);
PaginatedList = query.OrderBy(x => orderField).Skip(() => skipValue).Take(() => pageSize)
而且我得到这个错误:
"Unable to create a constant value of type [...]. Only primitive types or enumeration types are supported in this context"
我敢肯定有办法做到这一点,但目前不知道如何。
感谢所有的建议,我发现每个人的最佳解决方案是在答案为:http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet – Saleh
必须把信用发现重复问题的人正是我所需要的,非常感谢! !一直在寻找这个老旧的 – Saleh