How to convert Func <T, bool> to an expression <Func <T, bool >>

I have a Func like this:

 Func<MyClass, bool> func = x=>Id == 5;

How can I convert it to:

 Expression<Func<MyClass, bool>>
+5
source share
3 answers

Try the following:

Func<MyClass, bool> func = x=>Id == 5;
Expression<Func<MyClass, bool>> expr = mc => func(mc);
+3
source

You can simply write:

Expression<Func<MyClass,bool>> expr = x=>Id == 5;

This will set expras an expression tree for Id == 5.

If you do:

Func<MyClass, bool> func = x=>Id == 5;
Expression<Func<MyClass, bool>> expr = mc => func(mc);

Then it will set expras the expression tree for the call func, not the expression tree for the body func.

+9
source

, . , Expression s, , , (, SQL). , .

+2

All Articles