【发布时间】:2020-10-11 19:03:23
【问题描述】:
我有一个动态 Linq 查询
public static IQueryable<T> WhereHelper<T>(this IQueryable<T> source, string property, string propertyValue) where T : class
{
//STEP 1: Verify the property is valid
var searchProperty = typeof(T).GetProperty(property);
//STEP 2: Create the OrderBy property selector
var parameter = Expression.Parameter(typeof(T), "o");
MemberExpression member = Expression.Property(parameter, property);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
ConstantExpression constant = Expression.Constant(propertyValue);
MethodCallExpression queryExpr = Expression.Call(member, method, constant);
ParameterExpression pe = Expression.Parameter(typeof(string), property);
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { source.ElementType },
source.Expression,
Expression.Lambda<Func<string, bool>>(queryExpr, new ParameterExpression[] { pe }));
return source.Provider.CreateQuery<T>(whereCallExpression);
}
这个函数会产生如下错误:
“System.Linq.Queryable”类型上没有通用方法“Where” 与提供的类型参数和参数兼容。无类型 如果方法是非泛型的,则应提供参数。
关于错误的想法,谢谢
【问题讨论】: