This is a migrated thread and some comments may be shown as answers.

FilterDescriptors and Expressions

0 Answers 170 Views
DataFilter
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Yen-Sheng
Top achievements
Rank 1
Yen-Sheng asked on 10 Apr 2012, 03:27 AM
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.
Tags
DataFilter
Asked by
Yen-Sheng
Top achievements
Rank 1
Share this question
or