Reputation: 1368
I am building dynamic lamda expressions from string expressions using ParseAsExpression
. The problem is that i cannot figure out how to parse an expression of an array contains an object like mylist.Contains(x.Id)
Full example
var list = new int[] { 4,5,6};
var whereFunction = new Interpreter().SetVariable("mylist", list);
whereFunction.ParseAsExpression<Func<Person, bool>>("(person.Age == 5 && person.Name.StartsWith(\"G\")) || person.Age == 3 && mylist.Contains(person.Id)", "person");
Upvotes: 0
Views: 529
Reputation: 12209
I can confirm this is a bug: https://github.com/davideicardi/DynamicExpresso/issues/68
For now Array.Contains
doesn't work.
UPDATE:
Fixed in version 2.0.2.
Upvotes: 1
Reputation: 3230
For now you can do a workaround by implementing an alias extension method for each method doesn't work, like for Contains
=>Exists
:
var list = new int[] { 4,5,6};
var whereFunction = new Interpreter()
.SetVariable("mylist", list)
.Reference(typeof(ExtensionMethods));
whereFunction.ParseAsExpression<Func<Person, bool>>("(person.Age == 5 && person.Name.StartsWith(\"G\")) || person.Age == 3 && mylist.Exists(person.Id)", "person");
// Define this class somewhere
public static class ExtensionMethods
{
public static bool Exists<T>(this IEnumerable arr, T searchKey)
{
return ((IEnumerable<T>)arr).Contains(searchKey);
}
}
I see this is as a stupid workaround, but it'll work.
Upvotes: 2