在linq查询中设置动态排序名字段 [英] Setting a dynamic sort name field in a linq query
问题描述
我想要获得一个使用lambda表达式的 OrderBy
查询,以便我得到一个具有TOP(n)关键字的SQL查询(大的性能提升)
如果我指定...我可以这样做...
PaginatedList = query.OrderBy(x => x.QuoteID).Skip(()=> skipValue).Take(()=> pageSize)
pre>
但是,由于我希望orderBy字段通过一个名称的UI选择来动态化,我想做这样的事情:
var propertyInfo = typeof(Data.Quote).GetProperty(sortName);
表达式< Func< Data.Quote,object>> orderField = x => propertyInfo.GetValue(x,null);
PaginatedList = query.OrderBy(orderField).Skip(()=> skipValue).Take(()=> pageSize)
这给我错误:
LINQ to Entities不识别方法'系统.Object
GetValue(System.Object)'方法,此方法无法将
转换为存储表达式。
我试过这个不是类型
Expression< Func< T,object>>
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)
我收到此错误:
无法创建常量值类型[...]。只有原始类型
或枚举类型在此上下文中受支持
我是确定有一种方法来实现这一点,但目前还不确定如何。
解决方案这是如何实现你想要的:
var propertyInfo = typeof(Data.Quote).GetProperty(sortName);
ParameterExpression parameter = Expression.Parameter(typeof(T),s);
MemberExpression属性= Expression.Property(parameter,propertyInfo);
LambdaExpression sort = Expression.Lambda(property,parameter);
MethodCallExpression call = Expression.Call(
typeof(Queryable),
OrderBy,
new [] {typeof(T),property.Type}
Query.Expression,
Expression.Quote(sort));
var orderedQuery =(IOrderedQueryable< T>)Query.Provider.CreateQuery< T>(call);
PaginatedList = orderedQuery.Skip(skipValue).Take(pageSize);
I want to be able to get an
OrderBy
query working with a lambda expression so that I get a SQL query with the TOP(n) key word (big performance boost).I am able to do this if I specifiy ...
PaginatedList = query.OrderBy(x => x.QuoteID).Skip(() => skipValue).Take(() => pageSize)
But because I want the orderBy field to be dynamic through a UI selection of a name I want to do something like this:
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)
This gives me the error:
"LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression."
I tried this that's not of type
Expression<Func<T, object>>
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)
And I get this error:
"Unable to create a constant value of type [...]. Only primitive types or enumeration types are supported in this context"
I'm sure there is a way to achieve this but at the moment not sure how.
解决方案Here is how to achieve what you want:
var propertyInfo = typeof(Data.Quote).GetProperty(sortName); ParameterExpression parameter = Expression.Parameter(typeof(T), "s"); MemberExpression property = Expression.Property(parameter, propertyInfo); LambdaExpression sort = Expression.Lambda(property, parameter); MethodCallExpression call = Expression.Call( typeof(Queryable), "OrderBy", new[] {typeof(T), property.Type}, Query.Expression, Expression.Quote(sort)); var orderedQuery = (IOrderedQueryable<T>)Query.Provider.CreateQuery<T>(call); PaginatedList = orderedQuery.Skip(skipValue).Take(pageSize);
这篇关于在linq查询中设置动态排序名字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!