使用GetValue进行IQueryable 动态排序/过滤失败
我正在尝试使用Entity Framework CTP5从数据库中过滤结果。 这是我目前的方法。
IQueryable
但是,我发现GetValue不能使用Entity Framework。 它的类型是IEnumerable
但不是IQueryable
有没有我可以用来产生相同效果的替代方案?
public static IQueryable Like (this IQueryable source, string propertyName, string keyword) { Type type = typeof(T); ParameterExpression parameter = Expression.Parameter(type, "param"); MemberExpression memberAccess = Expression.MakeMemberAccess(parameter, type.GetProperty(propertyName)); ConstantExpression constant = Expression.Constant("%" + keyword + "%"); MethodInfo contains = memberAccess.Type.GetMethod("Contains"); MethodCallExpression methodExp = Expression.Call(memberAccess, contains, Expression.Constant(keyword)); Expression> lambda = Expression.Lambda>(methodExp, parameter); return source.Where(lambda); }
你会这样称呼它
Forms = Forms.Like(header, filter);
我没有对传递的参数进行任何validation。 例如,您必须validation您正在validation的属性类型上是否存在Contains方法。 所以它不适用于int
或类似的东西。