简体   繁体   English

Linq to SQL连接和位置

[英]Linq to SQL Join and Where

I have Users->Orders tables (one to many) and would like to select all users which have specified Orders. 我有Users-> Orders表(一对多),并希望选择所有已指定Orders的用户。 I have tried Linq below but it fails with error. 我在下面尝试过Linq,但是由于错误而失败。 How to write such Linq query? 如何编写这样的Linq查询?

DataAccess.Instance.Users.Where(p => p.Orders.Where(o => o.ProductId == productId))

You should try: 你应该试试:

DataAccess.Instance.Users.Where(p => p.Orders.Any(o => o.ProductId == productId))

Note that the second Where is changed to Any which returns a boolean value and satisfies the type of the expression tree expected by the first Where : Where needs a condition, not a set of values retrieved from elsewhere. 请注意,第二个Where更改为Any ,它将返回布尔值并满足第一个Where期望的表达式树的类型: Where需要条件,而不是从其他位置检索的一组值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM