How do I convert multiple inner joins in SQL to LINQ?

后端 未结 1 1406
名媛妹妹
名媛妹妹 2020-12-01 05:49

I\'ve got the basics of LINQ-to-SQL down, but I\'ve been struggling trying to get JOINs to work properly. I\'d like to know how to convert the following to LINQ-to-SQL (ide

相关标签:
1条回答
  • 2020-12-01 06:28

    Using query syntax:

    from c in dbo.Companies
    join p in dbo.Persons on c.AccountCoordinatorPersonId equals p.PersonId
    join p2 in dbo.Persons on c.AccountManagerPersonId equals p2.PersonId
    select new
    {
        c.CompanyId,
        c.CompanyName,
        AccountCoordinator = p.FirstName + ' ' + p.Surname,
        AccountManager = p2.FirstName + ' ' + p2.Surname
    }
    

    Using method chaining:

    dbo.Companies.Join(dbo.Persons, 
                       c => c.AccountCoordinatorPersonId,  
                       p => p.PersonId,  
                       (c, p) => new 
                       {  
                           Company = c,  
                           AccountCoordinator = p.FirstName + ' ' + p.Surname  
                       })
                 .Join(dbo.Persons,  
                       c => c.Company.AccountManagerPersonId,  
                       p2 => p2.PersonId,  
                       (c, p2) => new 
                       {  
                           c.Company.CompanyId,  
                           c.Company.CompanyName,  
                           c.AccountCoordinator,  
                           AccountManager = p2.FirstName + ' ' + p2.Surname 
                       });
    
    0 讨论(0)
提交回复
热议问题