Linq expression that would always return true

.net c# expression-trees linq

Question

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.

Popular Answer

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why