如何搜索ObservableCollection以进行文本匹配

时间:2014-04-05 04:06:38

标签: c# wpf

我想搜索一个可观察的集合进行文本匹配。该集合由一个包含大约五个属性的类组成。

我希望在不知道班级的具体属性名称的情况下我能做到这一点。这样我可以在任何集合上使用此搜索。

无论是字面上进行迭代搜索还是使用过滤器。

找到项目后,我将把一个始终存在的属性“IsSelected”设置为true;

这可能吗?如果是这样的话?


[编辑]尝试以下后,由于我的经验不足,我遇到了形成PredicateBuilder类的问题,不知道从哪里开始

User uvm = new User();
var yourObjectType = uvm.GetType();

var properties = yourObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty).Where(p => p.PropertyType == typeof(string)).Select(p => p.Name).ToList();
var param = System.Linq.Expressions.Expression.Parameter(yourObjectType, "p");
var seed = PredicateBuilder.False<User>();
var whereExpression = properties.Aggregate(seed, (p1, p2) => p1.Or(GetNavigationExpression(param, SearchTextboxValue, p2)));
var match = UserCollection.Where(whereExpression).ForEach(i => i.IsSelected = true);


[编辑2] 以下是GetNavigationExpression不在上下文中的问题。这是来自我的ViewModel,点击它执行的按钮。

private void SelectTextMatchCommandExecute(object parameter)
{
    string textSearchValue = (string)parameter;

    UserViewModel uvm = new UserViewModel();
    var yourObjectType = uvm.GetType();

    var properties = yourObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty).Where(p => p.PropertyType == typeof(string)).Select(p => p.Name).ToList();
    var param = System.Linq.Expressions.Expression.Parameter(yourObjectType, "p");
    var seed = PredicateBuilder.False<UserViewModel>();
    var whereExpression = properties.Aggregate(seed, (p1, p2) => p1.Or(GetNavigationExpression(param, textSearchValue, p2)));
    var match = UserCollection.Where(whereExpression).ForEach(i => i.IsSelected = true);
}

我已将您的所有建议放入以下内容中,我使用了来自here的GetNavigationExpressionProperties()副本,并将其重命名为GetNavigationProperties()。我只是假设它们都需要在一起,因为MakeLambda()使用ParameterVisitor()。

我想我在拼凑所有这些时遇到了麻烦

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace UserManagement
{
    /// <summary>
    /// Enables the efficient, dynamic composition of query predicates.
    /// </summary>
    public static class PredicateBuilder
    {
        /// <summary>
        /// Creates a predicate that evaluates to true.
        /// </summary>
        public static Expression<Func<T, bool>> True<T>() { return param => true; }

        /// <summary>
        /// Creates a predicate that evaluates to false.
        /// </summary>
        public static Expression<Func<T, bool>> False<T>() { return param => false; }

        /// <summary>
        /// Creates a predicate expression from the specified lambda expression.
        /// </summary>
        public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; }

        /// <summary>
        /// Combines the first predicate with the second using the logical "and".
        /// </summary>
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, System.Linq.Expressions.Expression.AndAlso);
        }

        /// <summary>
        /// Combines the first predicate with the second using the logical "or".
        /// </summary>
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, System.Linq.Expressions.Expression.OrElse);
        }

        /// <summary>
        /// Negates the predicate.
        /// </summary>
        public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
        {
            var negated = System.Linq.Expressions.Expression.Not(expression.Body);
            return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
        }

        /// <summary>
        /// Combines the first expression with the second using the specified merge function.
        /// </summary>
        static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {
            // zip parameters (map from parameters of second to parameters of first)
            var map = first.Parameters
                .Select((f, i) => new { f, s = second.Parameters[i] })
                .ToDictionary(p => p.s, p => p.f);

            // replace parameters in the second lambda expression with the parameters in the first
            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

            // create a merged lambda expression with parameters from the first expression
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

        class ParameterRebinder : ExpressionVisitor
        {
            readonly Dictionary<ParameterExpression, ParameterExpression> map;

            ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
            {
                this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
            }

            public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
            {
                return new ParameterRebinder(map).Visit(exp);
            }

            protected override Expression VisitParameter(ParameterExpression p)
            {
                ParameterExpression replacement;

                if (map.TryGetValue(p, out replacement))
                {
                    p = replacement;
                }

                return base.VisitParameter(p);
            }
        }

        private class ParameterVisitor : ExpressionVisitor
        {
            public Expression Parameter
            {
                get;
                private set;
            }
            protected override Expression VisitParameter(ParameterExpression node)
            {
                Parameter = node;
                return node;
            }
        }

        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            foreach (var item in source)
                action(item);

            return source;
        }

        public static Expression GetNavigationExpression(Expression parameter, int test, params string[] properties)
        {
            Expression resultExpression = null;
            Expression childParameter, navigationPropertyPredicate;
            Type childType = null;

            if (properties.Count() > 1)
            {
                //build path
                parameter = Expression.Property(parameter, properties[0]);
                var isCollection = typeof(IEnumerable).IsAssignableFrom(parameter.Type);
                //if it´s a collection we later need to use the predicate in the methodexpressioncall
                if (isCollection)
                {
                    childType = parameter.Type.GetGenericArguments()[0];
                    childParameter = Expression.Parameter(childType, childType.Name);
                }
                else
                {
                    childParameter = parameter;
                }
                //skip current property and get navigation property expression recursivly
                var innerProperties = properties.Skip(1).ToArray();
                navigationPropertyPredicate = GetNavigationExpression(childParameter, test, innerProperties);
                if (isCollection)
                {
                    //build methodexpressioncall
                    var anyMethod = typeof(Enumerable).GetMethods().Single(m => m.Name == "Any" && m.GetParameters().Length == 2);
                    anyMethod = anyMethod.MakeGenericMethod(childType);
                    navigationPropertyPredicate = Expression.Call(anyMethod, parameter, navigationPropertyPredicate);
                    resultExpression = MakeLambda(parameter, navigationPropertyPredicate);
                }
                else
                {
                    resultExpression = navigationPropertyPredicate;
                }
            }
            else
            {
                //Formerly from ACLAttribute
                var childProperty = parameter.Type.GetProperty(properties[0]);
                var left = Expression.Property(parameter, childProperty);
                var right = Expression.Constant(test, typeof(int));
                navigationPropertyPredicate = Expression.Equal(left, right);
                resultExpression = MakeLambda(parameter, navigationPropertyPredicate);
            }
            return resultExpression;
        }

        private static Expression MakeLambda(Expression parameter, Expression predicate)
        {
            var resultParameterVisitor = new ParameterVisitor();
            resultParameterVisitor.Visit(parameter);
            var resultParameter = resultParameterVisitor.Parameter;
            return Expression.Lambda(predicate, (ParameterExpression)resultParameter);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用here中的代码,你可以使用反射这样做:

var properties = yourObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty).Where(p => p.PropertyType == typeof(string)).Select(p => p.Name).ToList();
var parameter = Expression.Parameter(yourObjectType,"p");
var seed = PredicateBuilder.False<yourObjectType>();
var whereExpression = properties.Aggregate(seed, (p1,p2) => p1.Or(GetNavigationExpression(parameter, yourSearchTerm,p2));
var match = yourCollection.Where(whereExpression).ForEach(i => i.IsSelected = true);

编辑:PredicateBuilder取自here。 ForEach扩展可以从命名空间Microsoft.Practices.Prism.ObjectBuilder或

获取
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
    if (action == null)
        throw new ArgumentNullException("action");

    foreach (var item in source)
        action(item);

    return source;
}

如果要更改搜索谓词,则必须使其更具动态性。例如,使用像这样的枚举

public enum OperatorComparer
{
    Equals = ExpressionType.Equal,
    Contains,
    StartsWith,
    GreaterThan = ExpressionType.GreaterThan
}

  var childProperty = parameter.Type.GetProperty(properties[0]);
    var left = Expression.Property(parameter, childProperty);
    var right = Expression.Constant(test, typeof(int));
    if(!new List<OperatorComparer>{OperatorComparer.Contains,OperatorComparar.StartsWith}.Contains(operatorComparer))
   {
        navigationPropertyPredicate = Expression.MakeBinary((ExpressionType)operatorComparer,left, right);
   }
   else
   {
      var method = GetMethod(value, operatorComparer); //get property by enum-name from type
      navigationPropertyPredicate = Expression.Call(left, method, right);
   }
    resultExpression = MakeLambda(parameter, navigationPropertyPredicate);

答案 1 :(得分:1)

由于您正在处理内存中的集合,因此没有理由使用System.Linq.Expressions命名空间。当然,我知道您不希望使用反射来访问每个属性,但是您可以使用

public bool Match<T>(T item, string searchTeam)
{
    //You should cache the results of properties here for max perf.
    IEnumerable<Func<T, string>> properties = GetPropertyFunctions<T>();
    bool match = properties.Any(prop => prop(item) == "Foobar");
    return match;
}
public IEnumerable<Func<T, string>> GetPropertyFunctions<T>()
{
    var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty).Where(p => p.PropertyType == typeof(string)).ToList();
    var properties = propertyInfos.Select(GetProperyFunc<T>);
    return properties;        
}

public Func<T, string> GetProperyFunc<T>(PropertyInfo propInfo)
{
    ParameterExpression x = Expression.Parameter(typeof(User), "x");
    Expression<Func<User, string>> expression = Expression.Lambda<Func<User, string>>(Expression.Property(x, propInfo), x);
    Func<User, string> propertyAccessor = expression.Compile();
    return propertyAccessor;
}

然后我会使用LinqToObjects来处理这段代码。让LinqToObject编译器编译成你上面的表达式理论上会产生(非常轻微)更快的IL,但是你会在实际编译阶段丢失所有的perf。更不用说你可以缓存propertyAccessors的速度了。加上它更加简单的代码。

*注意这里的性能指标是每个属性访问器都包含在Func<User,string>中。 http://www.codeproject.com/Articles/584720/ExpressionplusbasedplusPropertyplusGettersplusandp

例如......

List<Func<User, string>> properties = ...
User user = ...
bool match = properties.Any(prop => prop(user) == "Foobar");