4

我正在为任何列/字段映射的查询中的 searchText 寻找通用过滤器

public static IQueryable<T> Filter<T>(this IQueryable<T> source, string searchTerm)
{
   var propNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(e=>e.PropertyType == typeof(String)).Select(x => x.Name).ToArray();


 //I am getting the property names but How can I create Expression for

 source.Where(Expression)
}

这里我给你一个示例场景

现在从我在 Asp.net MVC4 中的 HTML5 表中,我提供了一个搜索框来过滤输入文本的结果,它可以匹配以下任何列/菜单类属性值,我想在服务器端进行此搜索,如何我可以实施吗?

EF 模型类

 public partial class Menu
    {
        public int Id { get; set; }
        public string MenuText { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
        public string Icon { get; set; }
        public string ToolTip { get; set; }
        public int RoleId { get; set; }

        public virtual Role Role { get; set; }
    }
4

2 回答 2

5

您可以使用表达式:

private static Expression<Func<T, bool>> GetColumnEquality<T>(string property, string term)
{
    var obj = Expression.Parameter(typeof(T), "obj");        

    var objProperty = Expression.PropertyOrField(obj, property);
    var objEquality = Expression.Equal(objProperty, Expression.Constant(term));

    var lambda = Expression.Lambda<Func<T, bool>>(objEquality, obj);

    return lambda;
}

public static IQueryable<T> Filter<T>(IQueryable<T> source, string searchTerm)
{
    var propNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(e => e.PropertyType == typeof(string))
                             .Select(x => x.Name).ToList();

    var predicate = PredicateBuilder.False<T>();

    foreach(var name in propNames)
    {
        predicate = predicate.Or(GetColumnEquality<T>(name, searchTerm));
    }

    return source.Where(predicate);
}

PredicateBuilder在 NutShell 中结合来自 C# 的名称。这也是LinqKit的一部分。

例子:

public class Foo
{
    public string Bar { get; set; }
    public string Qux { get; set; }
}

Filter<Foo>(Enumerable.Empty<Foo>().AsQueryable(), "Hello");

// Expression Generated by Predicate Builder
// f => ((False OrElse Invoke(obj => (obj.Bar == "Hello"), f)) OrElse Invoke(obj => (obj.Qux == "Hello"), f))
于 2013-07-24T12:19:37.600 回答
3
void Main()
{
   // creates a clause like 
   // select * from Menu where MenuText like '%ASD%' or ActionName like '%ASD%' or....
    var items = Menu.Filter("ASD").ToList();
}

// Define other methods and classes here
public static class QueryExtensions
{
    public static IQueryable<T> Filter<T>(this IQueryable<T> query, string search)    
    {           
        var properties = typeof(T).GetProperties().Where(p => 
                /*p.GetCustomAttributes(typeof(System.Data.Objects.DataClasses.EdmScalarPropertyAttribute),true).Any() && */
                p.PropertyType == typeof(String));        
        
        var predicate = PredicateBuilder.False<T>();
        foreach (var property in properties )
        {
           predicate = predicate.Or(CreateLike<T>(property,search));
        }
        return query.AsExpandable().Where(predicate);
    }
    private static Expression<Func<T,bool>> CreateLike<T>( PropertyInfo prop, string value)
    {       
        var parameter = Expression.Parameter(typeof(T), "f");
        var propertyAccess = Expression.MakeMemberAccess(parameter, prop);                    
        var like = Expression.Call(propertyAccess, "Contains", null, Expression.Constant(value,typeof(string)));

        return Expression.Lambda<Func<T, bool>>(like, parameter);       
    }

}

您需要添加对LinqKit的引用才能使用 PredicateBuilder 和 AsExpandable 方法,否则无法使用 EF,只能使用 Linq to SQL

如果你愿意Col1 like '%ASD%' AND Col2 like '%ASD%' et,改变PredicateBuilder.FalsetoPredicateBuilder.Truepredicate.Ortopredicate.And

您还需要找到一种通过您自己的自定义属性(例如在部分类中定义)来区分映射属性的方法

于 2013-07-24T12:40:06.570 回答