The syntax of the query expression with the where clause (simplifying the full grammar)
from identifier in expression where boolean-expression select expression
whereClause not a boolean expression. To repeat this you must say
from c in _ctx.Companies where whereClause.Compile()(c) select c;
Note that if whereClause was Func<Company, bool> , you could leave with
from c in _ctx.Companies where whereClause(c) select c;
note that
from x in e where f
mechanically passed by the compiler to
(from x in e).Where(x => f)
I speak mechanically because he performs this translation without any semantic analysis to verify the correctness of method calls, etc. This step begins later after all query expressions have been translated into LINQ method call expressions.
In particular,
from c in _ctx.Companies where whereClause select c
translates to
_ctx.Companies.Where(c => whereClause).Select(c)
which is clearly pointless.
Reason for which
from c in _ctx.Companies.Where(whereClause) select c
is legal because IEnumerable<Company>.Where has an overload that accepts Func<Company, bool> , and there is an implicit conversion from Expression<Func<Company, bool>> to Func<Company, bool> .
jason
source share