使用 linq 连接查询
我在这里尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想在这里进行多对多加入吗,看起来是这样......
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
要按标签搜索事件,我认为您可以编写如下内容:
获取事件的标签:
对于后者,为了方便起见,您可以将其放在 Events 的属性中:
只需在需要的地方引用 myEvent.Tags 。
To search event by tag, I think you can write something like:
To get the tags for an event:
For the latter, for convenience, you can put it in a property of Events:
And just refer to myEvent.Tags where you need them.
要查找指定标签名称的事件名称,您可以执行以下操作:
同样,您可以搜索具有特定事件名称的标签,如下所示:
注意我如何从联接表开始,在具有已知值的表上联接和过滤,然后将搜索到的值加入到表中。
乔
To find the event names for a specified tag name, you could do this:
Similarly, you can search for tags with a specific event name like this:
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