EF 中的子查询噩梦

发布于 2024-11-08 04:03:38 字数 630 浏览 0 评论 0原文

我真的非常苦恼,除了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被翻牌 2024-11-15 04:03:38

试试这个(我猜这是你的 SQL 查询的 LINQ 解释):

int id = 123;
int version = 555;

var results = from p in context.Products 
              join pa in context.ProductApprovers 
              on p.ProductId = pa.ProductId 
              where p.ProductId equals id && pa.Version equals version
              select new { Product = p, Approver = pa };

Try this (I guess this is a LINQ interpretation of your SQL query):

int id = 123;
int version = 555;

var results = from p in context.Products 
              join pa in context.ProductApprovers 
              on p.ProductId = pa.ProductId 
              where p.ProductId equals id && pa.Version equals version
              select new { Product = p, Approver = pa };
小巷里的女流氓 2024-11-15 04:03:38

我怀疑你想要这样的东西:

var query = from product in db.Products
            where product.ProductId == productId
            select new { 
                Product = product,
                Approvers = product.Approvers.Where(pa => pa.Version == version)
            };

如果这不能满足你的要求,你能解释一下它在哪里失败吗?

I suspect you want something like this:

var query = from product in db.Products
            where product.ProductId == productId
            select new { 
                Product = product,
                Approvers = product.Approvers.Where(pa => pa.Version == version)
            };

If that doesn't do what you want, could you explain where it falls down?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文