IQueryable的<>动态排序/使用的GetValue过滤失败 [英] IQueryable<> dynamic ordering/filtering with GetValue fails

查看:121
本文介绍了IQueryable的<>动态排序/使用的GetValue过滤失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图筛选使用实体框架CTP5数据库结果。这里是我目前的方法

 的IQueryable<表格及GT; 。形式= DataContext.CreateFormContext()的形式; 
的foreach(在标题串标头){
形成= Forms.Where(F => f.GetType()
.GetProperty(头)
.GetValue(F,空)
的ToString()
.IndexOf(过滤器,
StringComparison.InvariantCultureIgnoreCase)> = 0);
}



不过,我发现的GetValue不使用实体框架的工作。它在类型,如果的IEnumerable<> 而不是的IQueryable<>



有没有我可以用它来产生同样效果的替代方案?


解决方案

 公共静态的IQueryable< T>像< T>(这IQueryable的< T>源字符串propertyName的,字符串关键字){$ B $型B型= ty​​peof运算(T); 
ParameterExpression参数= Expression.Parameter(类型,参数);
MemberExpression memberAccess = Expression.MakeMemberAccess(参数,type.GetProperty(propertyName的));

常量表达式常数= Expression.Constant(%+关键字+%);
MethodInfo的包含= memberAccess.Type.GetMethod(包含);

MethodCallExpression methodExp = Expression.Call(memberAccess,包含,Expression.Constant(关键字));
表达式来; Func键< T,BOOL>>波长= Expression.Lambda<&Func键LT; T,BOOL>>(methodExp,参数);
返回source.Where(拉姆达);
}

您会叫它像这样

 表格= Forms.Like(头,过滤器); 



我没有做过传递的参数的任何验证。比如你要验证该包含在你验证对属性的类型存在的方法。所以它不会在一个 INT 或类似的东西的工作。


I'm attempting to filter results from a database using Entity Framework CTP5. Here is my current method.

IQueryable<Form> Forms = DataContext.CreateFormContext().Forms;
foreach(string header in Headers) {
    Forms = Forms.Where(f => f.GetType()
                              .GetProperty(header)
                              .GetValue(f, null)
                              .ToString()
                              .IndexOf(filter,
                                  StringComparison.InvariantCultureIgnoreCase) >= 0);
}

However, I found that GetValue doesn't work using Entity Framework. It does when the type if IEnumerable<> but not IQueryable<>

Is there an alternative I can use to produce the same effect?

解决方案

public static IQueryable<T> Like<T>(this IQueryable<T> 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<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(methodExp, parameter);
    return source.Where(lambda);
}

You would call it like so

Forms = Forms.Like(header, filter);

I haven't done any validation of the passed parameters. For instance you have to validate that the Contains method exists on the type of the property you're validating against. So it won't work on an int or anything like that.

这篇关于IQueryable的&LT;&GT;动态排序/使用的GetValue过滤失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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