I created a filterable BindingList from this source. It works great:
list.Filter("Customer == 'Name'");
does what it should. The internals work like a parser, that converts the expression ==
or !=
into System.Linq.Expressions.Expression
. In this case, ==
becomes System.Linq.Expressions.Expression.Equal
.
Unfortunately System.Linq.Expressions.Expression
does not contain a like operator and I don't know how to solve this.
The initial code looks like this:
private static Dictionary<String, Func<Expression, Expression, Expression>>
binaryOpFactory = new Dictionary<String, Func<Expression, Expression, Expression>>();
static Init() {
binaryOpFactory.Add("==", Expression.Equal);
binaryOpFactory.Add(">", Expression.GreaterThan);
binaryOpFactory.Add("<", Expression.LessThan);
binaryOpFactory.Add(">=", Expression.GreaterThanOrEqual);
binaryOpFactory.Add("<=", Expression.LessThanOrEqual);
binaryOpFactory.Add("!=", Expression.NotEqual);
binaryOpFactory.Add("&&", Expression.And);
binaryOpFactory.Add("||", Expression.Or);
}
Then I created an expression that will do what I want:
private static System.Linq.Expressions.Expression<Func<String, String, bool>>
Like_Lambda = (item, search) => item.ToLower().Contains(search.ToLower());
private static Func<String, String, bool> Like = Like_Lambda.Compile();
e.g.
Console.WriteLine(like("McDonalds", "donAld")); // true
Console.WriteLine(like("McDonalds", "King")); // false
But binaryOpFactory
requires this:
Func<Expression, Expression, Expression>
The predefined expressions seem to be exactly that:
System.Linq.Expressions.Expression.Or;
Can anyone tell me how to convert my expression?
See Question&Answers more detail:
os