Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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());
}


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):
C#
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!
Posted
Updated 15-Aug-13 22:43pm
v3

1 solution

I managed to resolve my issue. It just happened to be somewhere else.
 
Share this answer
 
Comments
Richard C Bishop 4-Sep-13 15:30pm    
The appropriate content for solving your own issue would be to provide at least a link to where you found it or the code you got to work. Otherwise you are just talking to yourself.
nortee 4-Sep-13 15:40pm    
my solution had nothing to do with my expression tree I was building. It was an issue totally unrelated to the question that I posted. I apologise that I didn't note that in my initial reply. I just didn't want people to think I still had the problem. :)
Richard C Bishop 4-Sep-13 15:48pm    
It's all good, just a friendly comment so that you would not be down-voted into oblivion.
nortee 4-Sep-13 15:51pm    
Thanks m8 :) Duly noted :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900