I have defined a specification as an object of type Expression<Func<User, bool>>
like this:
public static Expression<Func<User, bool>> IsSystemUser
{
get
{
return user => user.UserID == -1;
}
}
This works marvellously with queries written in extension method syntax:
return workspace.GetDataSource<User>().Where(UserSpecifications.IsSystemUser);
But not with Linq query syntax:
return from user in workspace.GetDataSource<User>() where UserSpecifications.IsSystemUser select user;
The compiler gives me cannot implicitly convert type 'Expression<Func<User, bool>>' to 'bool'
.
What gives? I thought Linq query syntax was just a cute DSL dressing up the extension method syntax. Can anyone tell me how I might use my lovely specifications with Linq query syntax?
Your query expression is being translated into:
The lambda expression is introduced implicitly - but you don't want it in this case. So don't use query expression syntax... Given that here the query expression syntax is longer than using the extension methods directly, and introduces more cruft, why would you want it?
Note that you can mix and match like this: