PagedCollectionView过滤器的表达式树出现问题 [英] Problem with an expression tree for PagedCollectionView filter

查看:55
本文介绍了PagedCollectionView过滤器的表达式树出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试构建表达式谓词< object> (该对象可以是任何类类型)我想要应用于PagedCollectionView过滤器。我遇到的问题很奇怪。



基本上,我想(在这种情况下)检查类的字符串属性是否包含特定的子字符串,但是当我将谓词设置为Filter时PagedCollectionView的属性,我得到一个对象引用未设置为对象的实例异常,我有点难过。谁能帮我?此外,请注意,此树可以多次应用于同一个类(在不同的属性上)。



这是我正在测试的代码的一部分时刻:



Hi,

I am trying to build an expression tree Predicate<object> (the object can be any class type) which I want to apply to a PagedCollectionView filter. The issue which I am experiencing though, is pretty strange.

Basically, I want to (in this case) check a string property of a class if it contains a particular substring, but when I set the predicate to to the Filter property of a PagedCollectionView, I get a "Object reference not set to an instance of an object" exception and I am somewhat stumped. Can anyone help me? Also, please note that this tree can be applied multiple times for the same class (on different properties).

Here is the part of the code i am testing at the moment:

public static Predicate<object> GenerateContainsPredicateEx<C>(Type itemType, string propertyName, List<C> selectedValues, bool isNot)
{
    ParameterExpression objParam = System.Linq.Expressions.Expression.Parameter(typeof(object), "x");
    UnaryExpression param = System.Linq.Expressions.Expression.TypeAs(objParam, itemType);

    System.Linq.Expressions.Expression propertyExp = GetPropertyExpressionForObject(param, propertyName, itemType, typeof(C));
    System.Linq.Expressions.Expression condition = null;

    if (typeof(C) == typeof(String))
    {
        List<string> vals = (from s in selectedValues
                            select Convert.ToString(s))
                            .ToList();
        var propertyStringExp = ExpressionHelper.ToLower(ExpressionHelper.ToString(propertyExp));

        var valueExp = System.Linq.Expressions.Expression.Constant(vals[0].ToLower().Trim());

        MethodInfo containsMethod = typeof(String).GetMethods()
            .Where(m => m.Name == "Contains" && m.GetParameters().Length == 1)
            .Single();

        condition = System.Linq.Expressions.Expression.Call(propertyExp, containsMethod,
            new System.Linq.Expressions.Expression[] { valueExp });
    }
    if (condition == null)
        throw new NotImplementedException();

    System.Linq.Expressions.Expression finalCondition = condition;

    if (isNot)
        finalCondition = System.Linq.Expressions.Expression.Not(finalCondition);

    Expression<Func<object, bool>> equalfunction = System.Linq.Expressions.Expression.Lambda<Func<object, bool>>(finalCondition, objParam);

    return new Predicate<object>(equalfunction.Compile());
}





GetPropertyExpressionForObject 方法是我编码得到的正确方法该类的属性如果它是嵌套属性并且看起来像这样(这可能导致问题):



The GetPropertyExpressionForObject method is something I coded to get the correct property for the class if it's a nested property and looks like this (incase this might be causing the problem):

public static System.Linq.Expressions.Expression GetPropertyExpressionForObject(UnaryExpression objParam, string propertyName, Type originalType, Type objType)
        {
            Type type = originalType;
            System.Linq.Expressions.Expression propertyExp = System.Linq.Expressions.Expression.Convert(objParam, originalType);

            foreach (string level in propertyName.Split('.'))
            {
                propertyExp = System.Linq.Expressions.Expression.Property(propertyExp, type, level);                
                type = type.GetProperty(level).PropertyType;
            }

            return propertyExp;
        }





提前致谢!



Thanks in advance!

推荐答案

I设法解决我的问题。它碰巧在其他地方。
I managed to resolve my issue. It just happened to be somewhere else.


这篇关于PagedCollectionView过滤器的表达式树出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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