Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq expression that would always return true

I need to pass a parameter to a method that requires an Expression<Func<T, bool>>.

How to do I pass an expression that would always return true?

Using obj => true doesn't work because the framework complains at runtime that it cannot determine the memeber type from the True constant.

like image 724
Ilya Kogan Avatar asked Mar 31 '11 12:03

Ilya Kogan


2 Answers

If you have a function like this

void TakeExpression<T>(Expression<Func<T, bool>> expr)

You should call it this way, specifying the T type :

TakeExpression<int>(_ => true)

It should work.

like image 117
thinkbeforecoding Avatar answered Nov 15 '22 10:11

thinkbeforecoding


You need to define the parameter type you are passing:

(object o) => true 

Or

(int a) => true 
like image 43
Aliostad Avatar answered Nov 15 '22 08:11

Aliostad