Linq 到 SQL。多对多表连接

发布于 2024-10-06 17:14:27 字数 418 浏览 0 评论 0原文

我一直在构建一个电影应用程序来管理演员或“人才”。我有一个人才表和一个语言表。我还有一个 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 技术交流群。

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

发布评论

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

评论(2

咿呀咿呀哟 2024-10-13 17:14:27

您可以采用以下一种方法:

首先创建一个“结果”对象,该对象将在一个对象中保存您所需的信息。我们将其称为“TalentLanguagesContainer”

public class TalentLanguagesContainer
{
    public int TalentID { get; set; }
    public string FirstName { get; set; }
    public int LanguageID { get; set; }
    public string LanguageName { get; set; }
}

然后,创建一个 Select 语句来适当地映射您的需求:

public IQueryable < TalentLanguagesContainer > GetTalentLanguages()
{           
       MyDataContext _dataContext = new MyDataContext();

       return _dataContext.TalentLanguages
                  .Where(t => t.TalentID == 10000)
                  .Select(tl => new TalentLanguagesContainer() {
                             TalentID = tl.TalentID,
                             FirstName = tl.Talent.FirstName,
                             LanguageID = tl.LanguageID,
                             LanguageName = tl.Language.Name });
}

此外,您可能需要考虑为更复杂的脚本(例如此脚本)编写存储过程 - 您可能会发现 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"

public class TalentLanguagesContainer
{
    public int TalentID { get; set; }
    public string FirstName { get; set; }
    public int LanguageID { get; set; }
    public string LanguageName { get; set; }
}

Then, create a Select statement that will map your needs appropriately:

public IQueryable < TalentLanguagesContainer > GetTalentLanguages()
{           
       MyDataContext _dataContext = new MyDataContext();

       return _dataContext.TalentLanguages
                  .Where(t => t.TalentID == 10000)
                  .Select(tl => new TalentLanguagesContainer() {
                             TalentID = tl.TalentID,
                             FirstName = tl.Talent.FirstName,
                             LanguageID = tl.LanguageID,
                             LanguageName = tl.Language.Name });
}

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.

你怎么这么可爱啊 2024-10-13 17:14:27

Remus,我想我会自己回答这个问题,因为这是一个非常干净的解决方案。看看这个...

var languages = from tl in talentDB.TalentLanguages
                where tl.TalentID == id
                select new { lang = tl.Language.Name, tal_id = tl.TalentID };  // could get more values if needed..

foreach (var l in languages)
{
    string language = l.lang;
    string talentID = l.tal_id; 
    // etc...
}

这太酷了! Linq 帮我加入了!!

Remus, I think I'm gonna answer this myself because it's such a clean solution. Check this out...

var languages = from tl in talentDB.TalentLanguages
                where tl.TalentID == id
                select new { lang = tl.Language.Name, tal_id = tl.TalentID };  // could get more values if needed..

foreach (var l in languages)
{
    string language = l.lang;
    string talentID = l.tal_id; 
    // etc...
}

This is pretty cool! Linq did the join for me!!

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