将两个表达式组合在一起?

时间:2019-03-08 08:09:13

标签: c# expression

我正在构建一个将字符串转换为lambda表达式的应用程序。例如,如果输入是"c.id>7 && c.grade<5"之类的字符串,则应将其转换为c=>c.id>7 && c.grade<5;如果输入是像"c.id>7 && u.grade<5"这样的字符串,我应该将其转换为(c,u)=>c.id>7 && u.grade<5

我已经构建了一个应用程序来识别字符串"c.id>7 && c.grade<5",并将其转换为两个表达式c=>c.id>7c=>c.grade<5。但是,当我尝试将两个表达式组合在一起时,如下所示:

// expression1 is c=>c.id>7, expression1 is return by method Expression.Lambda
// expression2 is c=>c.grade<5 or u.grade<5, expression2 is return by method Expression.Lambda
Expression.AndAlso(expression1, expression2);

系统说

there is no binary opertor AndAlso between System.Func`2[Test.user, System.Boolean] and System.Func`2[Test.user, System.Boolean]

那么谁能帮助我将两个表达式结合在一起,谢谢!

ps:

// expression1 and expression2 is built like below    
Expression expression = Expression.Lambda(Expression.AndAlso(leftExpression, rightExpression), parameterExpressions.ToArray());

0 个答案:

没有答案