实体框架 4 中的多对多查询

发布于 2024-10-31 11:50:13 字数 708 浏览 1 评论 0原文

我的数据库中有多对多关系。两个端表是 BlogPost 和 Item,中间的表是 ItemBlogPost。我需要取回与特定项目相关的所有博客文章。在 SQL 中,我会这样做:

SELECT BlogPost.*
FROM BlogPost
    JOIN ItemBlogPost ON BlogPost.ID = ItemBlogPost.BlogPost_ID
WHERE ItemBlogPost.Item_ID = @Item_ID

在 C# 中,我有类似的东西:

IQueryable<BlogPost> itemBlogPosts = from b in connection.BlogPosts
                                     where b.Items == item.ID 
                                     orderby b.Content.CreateDate descending
                                     select b;

但是,标记为 b.Items 的行没有给我 Item 属性的列表,并且没有 b.ItemBlogPost 来查看中间表。我也尝试过执行 b.Items.Contains(item) 但也失败了。如何在 LINQ to EF4 中实现此功能?

I have have a many to many relationship in my database. The two end tables are BlogPost and Item and the table in the middle is ItemBlogPost. I need to get back all of the BlogPosts related to a specific item. In SQL I would do it like this:

SELECT BlogPost.*
FROM BlogPost
    JOIN ItemBlogPost ON BlogPost.ID = ItemBlogPost.BlogPost_ID
WHERE ItemBlogPost.Item_ID = @Item_ID

In C# I have something similar:

IQueryable<BlogPost> itemBlogPosts = from b in connection.BlogPosts
                                     where b.Items == item.ID 
                                     orderby b.Content.CreateDate descending
                                     select b;

However, the line marked b.Items doesn't give me a list of the Item properties and there is no b.ItemBlogPost to look at the intermediary table. I also tried doing b.Items.Contains(item) but that also failed. How can I make this work in LINQ to EF4?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

意中人 2024-11-07 11:50:13

怎么样:

var itemBlogPosts = from i in connection.Items
                    from b in i.BlogPosts // I suppose you have a nav. property on Item
                    where i.Id == itemId
                    select b; 

同样的查询也可以通过以下方式定义:

var itemBlogPosts = connection.Items
                              .Where(i => i.Id == itemId)
                              .SelectMany(i => i.BlogPosts);

What about this:

var itemBlogPosts = from i in connection.Items
                    from b in i.BlogPosts // I suppose you have a nav. property on Item
                    where i.Id == itemId
                    select b; 

The same query can be also defined by:

var itemBlogPosts = connection.Items
                              .Where(i => i.Id == itemId)
                              .SelectMany(i => i.BlogPosts);
浅沫记忆 2024-11-07 11:50:13

您可以这样做吗:

var itemBlogPosts = connection.Items.Single(b => b.ID == item.ID).BlogPosts;

由于您使用的是 EF,它应该为您处理多对多映射,并且您应该将 BlogPosts 作为 Item 对象中的导航项。

Can you just do this:

var itemBlogPosts = connection.Items.Single(b => b.ID == item.ID).BlogPosts;

Since you are using EF it should handle the many-to-many mapping for you and you should have BlogPosts as a navigation item in your Item Object.

亣腦蒛氧 2024-11-07 11:50:13

如果:

  1. 您从数据库生成了一个模型(因此不是模型优先)
  2. 连接表恰好包含两个外键(没有附加列)
  3. 外键以正确的方式设置

然后:

  1. EF 将已为您生成了包含“两侧”所谓的导航属性的类。 BlogPost 上的导航属性(项目集合)和 nav.prop。 (博客文章的集合)在项目上。

这样:

  1. 您可以双向遍历对象图。获取特定项目的所有博客文章,或者以相反的方式获取特定博客文章的所有项目。

因此,当您手动拥有特定项目时,您可以通过执行以下操作来创建相关博客文章的集合:

Item item = context.Items.Include("BlogPosts").FirstOrDefault<Item>(i => i.ID = someID)

现在您拥有一个特定项目,其中填充了博客文章的集合(如果有与该项目相关的内容)。

If:

  1. You generated a model from the database (so not model-first)
  2. The connecting table contains exactly two foreign keys (without additional columns)
  3. The foreign keys were set up in the right way

Then:

  1. EF would have generated classes for you that contain so-called navigation properties on "both sides". A navigation property (collection of Items) on BlogPost and a nav.prop. (collection of BlogPosts) on Item.

This way:

  1. You can traverse the object graph bidirectional. Getting all the blogposts for a specific item or the other way around getting all the items for a certain blogpost.

So when you have your specific item by hand you can just create a collection of related blogposts by doing:

Item item = context.Items.Include("BlogPosts").FirstOrDefault<Item>(i => i.ID = someID)

Now you have a specific item with the collection of blogposts filled (if any were related to this item).

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