简体   繁体   中英

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.

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.

You need to define the parameter type you are passing:

(object o) => true 

Or

(int a) => true 

There are two problems here:

1) If you're passing a predicate such that you always want to return true, then it's not much of a predicate. You may be able to omit whatever call you are trying to make.

2) If you want to just return true, you can simple use a more verbose lambda syntax to get what you want:

sample.AsQueryable().Where((x) => { return true; });

The more verbose syntax allows you to specify closer to an anonymous function while still being an expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM