Linq:查询语法,其中运算符不理解表达式类型的谓词
我已将规范定义为 Expression
类型的对象,如下所示:
public static Expression<Func<User, bool>> IsSystemUser
{
get
{
return user => user.UserID == -1;
}
}
这对于以扩展方法语法编写的查询效果非常好:
return workspace.GetDataSource<User>().Where(UserSpecifications.IsSystemUser);
但不适用于 Linq 查询语法:
return from user in workspace.GetDataSource<User>() where UserSpecifications.IsSystemUser select user;
编译器给我 cannot 隐式转换类型 'Expression
什么给?我认为 Linq 查询语法只是一个可爱的 DSL,装饰了扩展方法语法。谁能告诉我如何将我可爱的规范与 Linq 查询语法结合使用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询表达式将被转换为:
lambda 表达式是隐式引入的 - 但在这种情况下您不需要它。所以不要使用查询表达式语法...鉴于这里的查询表达式语法比直接使用扩展方法更长,并且引入了更多的麻烦,你为什么需要它?
请注意,您可以像这样混合搭配:
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: