使用 linq 连接查询

发布于 2024-07-21 02:20:57 字数 310 浏览 12 评论 0原文

我在这里尝试在 linq 查询中进行一些左连接,但我想说我不知道​​如何实现这个想法。

基本上这是我想要使用的 3 个数据库结构。

<tags>
id | name

<events_tags>
tag_id | event_id

<events>
id | name | some-other-fields

因此,对于每个事件,都与标签存在一对多关系,一个事件可以有一个或多个标签。

我想知道如何根据标签搜索事件,或者如何根据事件 ID 知道关联的标签?

I am trying here to make a few left joins into a linq query but I'd say I rather have no idea how to materialize this idea.

Basically here is the 3 database structures I want to play with.

<tags>
id | name

<events_tags>
tag_id | event_id

<events>
id | name | some-other-fields

so for each events there is a one-to-many relation with tags, an event can then have one or more tags.

I'd like to know how to search an event based on a tag or how can I, based from an event id know the associated tags ?

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

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

发布评论

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

评论(3

心奴独伤 2024-07-28 02:20:58

您想在这里进行多对多加入吗,看起来是这样......
Linq to sql 不支持这个...这是一篇很棒的文章

http://blogs.msdn.com/mitsu/archive/2007/06/21/how-to-implement-a -many-to-many-relationship-using-linq-to-sql.aspx

Scott Guthrie 的这个对于掌握基础知识非常有用

http://weblogs.asp.net/scottgu/archive/2007/05 /19/using-linq-to-sql-part-1.aspx

希望有帮助

Are you wanting to do a many to many join here, looks that way....
Linq to sql does not support this...here is a great article

http://blogs.msdn.com/mitsu/archive/2007/06/21/how-to-implement-a-many-to-many-relationship-using-linq-to-sql.aspx

And this one from Scott Guthrie is useful in getting to grips with the basics

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

hope that helps

晨与橙与城 2024-07-28 02:20:58

要按标签搜索事件,我认为您可以编写如下内容:

var tagsIds = from t in DataContext.Tags
              where t.Name == "sometag"
              select t.id;

var eventsByTag = from et in DataContext.EventTags
                  where tagsIds.Contains(et.tag_id)
                  select et.Event;

获取事件的标签:

var tagsByEvent = from et in myEvent.EventTags
                  select et.Tag;

对于后者,为了方便起见,您可以将其放在 Events 的属性中:

public List<Tag> Tags
{
   get
   {
      List<Tag> tags = (from et in this.EventTags
                        select et.Tag).ToList();
      return tags;
   }
}

只需在需要的地方引用 myEvent.Tags 。

To search event by tag, I think you can write something like:

var tagsIds = from t in DataContext.Tags
              where t.Name == "sometag"
              select t.id;

var eventsByTag = from et in DataContext.EventTags
                  where tagsIds.Contains(et.tag_id)
                  select et.Event;

To get the tags for an event:

var tagsByEvent = from et in myEvent.EventTags
                  select et.Tag;

For the latter, for convenience, you can put it in a property of Events:

public List<Tag> Tags
{
   get
   {
      List<Tag> tags = (from et in this.EventTags
                        select et.Tag).ToList();
      return tags;
   }
}

And just refer to myEvent.Tags where you need them.

命比纸薄 2024-07-28 02:20:58

要查找指定标签名称的事件名称,您可以执行以下操作:

Console.WriteLine("\nEvents tagged as .NET:\n");

(from evtTag in ctx.EventsTags
 join tag in ctx.Tags on evtTag.TagID equals tag.ID
 where tag.Name == ".NET"
 join evt in ctx.Events on evtTag.EventID equals evt.ID
 select evt)
 .ToList()
 .ForEach(evt => Console.WriteLine(evt.Name));

同样,您可以搜索具有特定事件名称的标签,如下所示:

Console.WriteLine("\nTags for TechEd:\n");

(from evtTag in ctx.EventsTags
 join evt in ctx.Events on evtTag.EventID equals evt.ID
 where evt.Name == "TechEd"
 join tag in ctx.Tags on evtTag.TagID equals tag.ID
 select tag)
 .ToList()
 .ForEach(tag => Console.WriteLine(tag.Name));

注意我如何从联接表开始,在具有已知值的表上联接和过滤,然后将搜索到的值加入到表中。

To find the event names for a specified tag name, you could do this:

Console.WriteLine("\nEvents tagged as .NET:\n");

(from evtTag in ctx.EventsTags
 join tag in ctx.Tags on evtTag.TagID equals tag.ID
 where tag.Name == ".NET"
 join evt in ctx.Events on evtTag.EventID equals evt.ID
 select evt)
 .ToList()
 .ForEach(evt => Console.WriteLine(evt.Name));

Similarly, you can search for tags with a specific event name like this:

Console.WriteLine("\nTags for TechEd:\n");

(from evtTag in ctx.EventsTags
 join evt in ctx.Events on evtTag.EventID equals evt.ID
 where evt.Name == "TechEd"
 join tag in ctx.Tags on evtTag.TagID equals tag.ID
 select tag)
 .ToList()
 .ForEach(tag => Console.WriteLine(tag.Name));

Notice how I started with the join table, joined and filtered on the table with the known value, and then joined to the table with the values searched for.

Joe

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