EF 中的子查询噩梦
我真的非常苦恼,除了 LINQ(例如 SQL!)之外,
我有两个实体:
Product
ProductApprover
Product 实体在 ProductApprover 实体上有一对多关系,例如:
Product.ProductApprovers为我提供与产品相关的所有 ProductApprover 实体。
当通过我的产品实体上的 ProductID 列进行查询时,获取产品和关联的 ProductApprover 数据非常简单,因为关联的 ProductApprover 数据会自动捆绑到结果中,但是当我想通过查询关联的 ProductApprover 内的数据来更改查询时,我的问题就出现了实体。我尝试了各种使用“Where”、“Contains”和“Any”函数等来执行子查询,但似乎无法获得我想要的结果。
我想要执行的查询是:
SELECT * FROM Product p
INNER JOIN ProductApprover pa ON p.ProductId = pa.ProductId
WHERE p.ProductId = @id AND pa.Version = @version
有人可以帮我吗?预先感谢您。
I'm really, really struggling with what should otherwise be a straightforward query in anything other than LINQ (for example SQL!)
I have two entities:
Product
ProductApprover
The Product entity has a one to many relationship on the ProductApprover entity, e.g:
Product.ProductApprovers gives me all ProductApprover entities relating to the Product.
Getting a Product and associated ProductApprover data is simple enough when querying by my ProductID column on my Product entity as the ProductApprover data associated is bundled into the result automatically, but my problem comes when I want to alter my query by querying data WITHIN my associated ProductApprover entities. I have tried all sorts with use of the 'Where', 'Contains' and 'Any', functions, etc, to perform a subquery, but cannot seem to get the result I want.
The query I want to perform is:
SELECT * FROM Product p
INNER JOIN ProductApprover pa ON p.ProductId = pa.ProductId
WHERE p.ProductId = @id AND pa.Version = @version
Can anybody help me out please? Thank you kindly in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个(我猜这是你的 SQL 查询的 LINQ 解释):
Try this (I guess this is a LINQ interpretation of your SQL query):
我怀疑你想要这样的东西:
如果这不能满足你的要求,你能解释一下它在哪里失败吗?
I suspect you want something like this:
If that doesn't do what you want, could you explain where it falls down?