Linq 到 SQL。多对多表连接
我一直在构建一个电影应用程序来管理演员或“人才”。我有一个人才表和一个语言表。我还有一个 TALENTLANGUAGES 表,显示两者之间的多对多关系。
这是我可以编写的 SQL 来显示给定人才使用的不同语言。
Select t.TalentID, t.FirstName, tl.LanguageID, l.Name from Talent t
inner join TalentLanguage tl on tl.TalentID = t.TalentID
inner join Language l on l.LanguageID = tl.LanguageID
where t.TalentID = 10000;
我在我的 C# 应用程序中使用 Linq to sql 类。我如何使用 linq to sql 执行上述代码。谢谢。
I have been building a Motion Picture application to manage actors or "Talents". I have a TALENT table and a LANGUAGES table. I also have a TALENTLANGUAGES table that shows the many to many relationship between the two.
Here is the SQL i can write to show the different languages a given talent speaks.
Select t.TalentID, t.FirstName, tl.LanguageID, l.Name from Talent t
inner join TalentLanguage tl on tl.TalentID = t.TalentID
inner join Language l on l.LanguageID = tl.LanguageID
where t.TalentID = 10000;
Im in my C# application I'm using Linq to sql classes. How might I do the above code with linq to sql. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以采用以下一种方法:
首先创建一个“结果”对象,该对象将在一个对象中保存您所需的信息。我们将其称为“TalentLanguagesContainer”
然后,创建一个 Select 语句来适当地映射您的需求:
此外,您可能需要考虑为更复杂的脚本(例如此脚本)编写存储过程 - 您可能会发现 SQL 脚本也执行得更快。
Here's one way you can do it:
Start by creating a "results" object, something that will hold the information that you need in one object. Let's call it "TalentLanguagesContainer"
Then, create a Select statement that will map your needs appropriately:
Also, you may want to consider writing stored procedures for more complex scripts such as this one - you may find an SQL script to perform faster too.
Remus,我想我会自己回答这个问题,因为这是一个非常干净的解决方案。看看这个...
这太酷了! Linq 帮我加入了!!
Remus, I think I'm gonna answer this myself because it's such a clean solution. Check this out...
This is pretty cool! Linq did the join for me!!