C# 将包含特定标识符的数组解析为表达式

C# 将包含特定标识符的数组解析为表达式,c#,.net,dynamic-expresso,C#,.net,Dynamic Expresso,我正在使用parseexpression从字符串表达式构建动态lamda表达式。问题是,我无法理解如何解析包含类似mylist.contains(x.Id) 完整示例 var list = new int[] { 4,5,6}; var whereFunction = new Interpreter().SetVariable("mylist", list); whereFunction.ParseAsExpression<Func<Person, bool>&g

我正在使用
parseexpression
从字符串表达式构建动态lamda表达式。问题是,我无法理解如何解析包含类似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");
var list=newint[]{4,5,6};
var whereFunction=new解释器().SetVariable(“mylist”,列表);
其中function.parseExpression(((person.Age==5&&person.Name.StartsWith(\“G\”)| | person.Age==3&&mylist.Contains(person.Id)”,“person”);

我可以确认这是一个错误:

目前,
Array.Contains
不起作用

更新:


已在2.0.2版中修复。

我可以确认这是一个错误:

目前,
Array.Contains
不起作用

更新:


已在2.0.2版中修复。

现在,您可以通过为每个方法都不起作用的情况实施别名扩展方法来解决问题,例如对于
包含
=>
存在

 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);
    }
}
var list=newint[]{4,5,6};
var whereFunction=new解释器()
.SetVariable(“mylist”,列表)
.参考(类型(扩展方法));
其中function.parseExpression(((person.Age==5&&person.Name.StartsWith(\“G\”)| | person.Age==3&&mylist.Exists(person.Id)”,“person”);
//在某个地方定义这个类
公共静态类扩展方法
{
存在公共静态bool(此IEnumerable arr,T searchKey)
{
返回((IEnumerable)arr).Contains(searchKey);
}
}

我认为这是一个愚蠢的解决方法,但它会起作用。

现在,您可以通过实现别名扩展方法来解决每个方法都不起作用的问题,比如对于
包含
=>
存在

 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);
    }
}
var list=newint[]{4,5,6};
var whereFunction=new解释器()
.SetVariable(“mylist”,列表)
.参考(类型(扩展方法));
其中function.parseExpression(((person.Age==5&&person.Name.StartsWith(\“G\”)| | person.Age==3&&mylist.Exists(person.Id)”,“person”);
//在某个地方定义这个类
公共静态类扩展方法
{
存在公共静态bool(此IEnumerable arr,T searchKey)
{
返回((IEnumerable)arr).Contains(searchKey);
}
}

我认为这是一个愚蠢的解决方法,但它会起作用。

这是什么语言?Array.Contains bug现在已经修复,从DynamicXpress2.0.2版开始,这是什么语言?Array.Contains bug现在已经修复,从DynamicExpress的2.0.2版开始,如果此解决方案适用于您,请将其作为正确答案进行检查。好的,我检查了它,但事实上,它不适用于我的场景。尽管它创建了表达式,但不能在Servicestack或entity framework中用作sql表达式。等待修复…它肯定不适用于SQL,您没有提到您希望它适用于SQL。此解决方案仅适用于linq to object。如果此解决方案适用于您,请将其作为正确答案进行检查。好的,我检查了它,但事实上,它不适用于我的场景。尽管它创建了表达式,但不能在Servicestack或entity framework中用作sql表达式。等待修复…它肯定不适用于SQL,您没有提到您希望它适用于SQL。此解决方案仅适用于linq to对象。