0

I'm building an application to translate string to lambda expression. For example, if the input is a string like "c.id>7 && c.grade<5" , I should translate it to c=>c.id>7 && c.grade<5; if the input is string like "c.id>7 && u.grade<5" ,I should translate it to (c,u)=>c.id>7 && u.grade<5.

I'v built an application to recognize the string "c.id>7 && c.grade<5" ,and translate it to two expressions c=>c.id>7 and c=>c.grade<5. But when I try to combine the two expressions together like below:

// 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);

System said

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

So who can help me combine the two expressions together, thanks!

ps:

// expression1 and expression2 is built like below    
Expression expression = Expression.Lambda(Expression.AndAlso(leftExpression, rightExpression), parameterExpressions.ToArray());
user2155362
  • 1,657
  • 5
  • 18
  • 30
  • 2
    What are the types of `expressions1` and `expressions2`? – Lasse V. Karlsen Mar 08 '19 at 08:12
  • @Lasse Vågsæther Karlsen , expression1 and expression2 are built by method Expression.Lambda – user2155362 Mar 08 '19 at 08:15
  • I still want you to show exactly what the types are, please show the declarations of `expressions1` and `expressions2`, if you're using `var`, please get Visual Studio to replace it with the actual type, then show the declarations. – Lasse V. Karlsen Mar 08 '19 at 08:24
  • Hope these items helps you: https://stackoverflow.com/a/457328/4373895 AND https://stackoverflow.com/a/10613631/4373895 – VikrantMore Mar 08 '19 at 08:29
  • Maybe try [Combining two `Expression>`](https://stackoverflow.com/a/457328/7571171) ? Also, please add some more information to your question (i.e. what are the types of `leftExpression` and `rightExpression`?). See [MVCE](https://stackoverflow.com/help/mcve). – Thomas Flinkow Mar 08 '19 at 08:30
  • @Thomas Flinkow, the reason I post the question is I think it is not as simple as combine two Expression>. I don't know which type of lambda expression I need to combine. This time, I need combine Expression> with Expression> to Func, and next time probably I need combine Expression> with Expression> to Func. This is totally decide at running time. – user2155362 Mar 08 '19 at 08:38
  • How many input parameters can accept lambda? You provided examples with 1-2 params, but can it accept more? How do you decide what lambda input param is basing on input string? I mean, how do you know what `c` and `u` input params should be? – ingvar Mar 08 '19 at 10:54

0 Answers0