0

我正在使用字符串表达式构建动态 lamda 表达式 ParseAsExpression。问题是我无法弄清楚如何解析一个数组的表达式包含一个像这样的对象mylist.Contains(x.Id)

完整示例

 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");
4

2 回答 2

2

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.

于 2017-10-10T08:15:11.493 回答
1

我可以确认这是一个错误:https ://github.com/davideicardi/DynamicExpresso/issues/68

暂时Array.Contains行不通。

更新:

已在 2.0.2 版中修复。

于 2017-10-09T14:54:25.200 回答