FilterDescriptors and Expressions

1 posts, 0 answers
  1. Yen-Sheng
    Yen-Sheng avatar
    13 posts
    Member since:
    Aug 2011

    Posted 09 Apr 2012 Link to this post

    I want to be able to filter information out of a list of dictionaries.

    Code as follows

    List<Dictionary<string, int>> data = new List<Dictionary<string, int>>();
    data.Add(new Dictionary<string, int>() { { "col1", 99 }, { "col2", 99 } });
    data.Add(new Dictionary<string, int>() { { "col1", 101 }, { "col2", 101 } });
    data.Add(new Dictionary<string, int>() { { "col1", 99 }, { "col2", 201 } });
     
                
    FilterDescriptor filterDescriptor = new FilterDescriptor("col1", FilterOperator.IsLessThanOrEqualTo, 100);
    filterDescriptor.MemberType = typeof(int);
    ParameterExpression values = Expression.Parameter(typeof(Dictionary<string, int>));
     
    Expression expression = filterDescriptor.CreateFilterExpression(values);
    var compiledExpression = Expression.Lambda<Func<Dictionary<string, int>, bool>>(expression, values).Compile();
    foreach(var item in data)
    {
        System.Diagnostics.Debug.WriteLine(item["col1"]);
        if (compiledExpression.Invoke(item))
        {
            System.Diagnostics.Debug.WriteLine("true");
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("false");
        }
    }


    It gives me all trues :(

    I'm clearly missing something here. 
    Ideally, I would like to latch onto "CreateFilterExpression" else I would just create one from scratch which would be rather pointless if you guys have a better solution.
Back to Top