score:18

Accepted answer

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

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

I would personally then remove the "== true" redundancy (I know this was only sample code, but...):

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

I'd then consider what using a query expression is actually buying you. If you're just doing a "where" (or just doing a "select") you may find dot notation simpler:

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

It gets even better though: the compiler doesn't need to infer a return value from the lambda expression (because it will always be bool) so you can just use a method group conversion:

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

How much cleaner is that? :)


Related Articles