Linq Evaluating a Method as a Lambda Expression

I am trying to select from a list using the linq expression where the range variable is evaluated in the static method that returns boolean. I would like to select a range variable that returns true when the range variable is evaluated using the method.

var result = from rangeVariable in DataSource
             where (rangeVariable => Foo.MethodReturnsBoolean(rangeVariable) == true)
             select rangeVariable;

I get this error:

It is not possible to convert a Lambda expression to type 'bool' because it is not a delegate type.

Can anyone explain what is happening and how could I achieve this?

+3
source share
1 answer

You don't need the lambda expression in the where clause - translating the query expression does this for you. Just use:

var result = from rangeVariable in DataSource
             where Foo.MethodReturnsBoolean(rangeVariable) == true
             select rangeVariable;

"== true" ( , , ...):

var result = from rangeVariable in DataSource
             where Foo.MethodReturnsBoolean(rangeVariable)
             select rangeVariable;

, . "where" ( "" ), :

var result = DataSource.Where(x => Foo.MethodReturnsBoolean(x));

: ( bool), :

var result = DataSource.Where(Foo.MethodReturnsBoolean);

?:)

+18

Source: https://habr.com/ru/post/1713206/


All Articles