Solved

LINQ Query Joining same table twice

Posted on 2013-10-25
4
1,195 Views
Last Modified: 2016-02-10
I am having trouble with a LINQ query to return the name of the Home and Away team from the same table.  I have  schedule table with awayid and homeid.  In sql server it's easy to reference the teams table as teams and teams1 and get my data in same record.  I cannot figure out the LINQ code to produce same results.  

This brings in home team name.  How would I change it to also get away team from teams table.

 Dim curScores = From s In db.Schedules Join hteams In db.Teams On s.HomeID Equals hteams.Teamid
                Where s.Week = 3 And s.Season = "2014" Select Teamid = s.HomeID, HomeTeam = hteams.TeamName
0
Comment
Question by:Break40
  • 3
4 Comments
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 500 total points
ID: 39602038
You need to do something like:

From
      s In db.Schedules
      Join t1 hteams In db.Teams
            On s.HomeID Equals t1.Teamid
      Join t2 hteams In db.Teams
            On s.HomeID Equals t2.Teamid
        Where s.Week = 3 And s.Season = "2014" Select Teamid = s.HomeID, HomeTeam = t1.TeamName
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39602145
If I understand the issue correctly, then one of Zberteoc's Join statements needs to reference the s.AwayID rather than both pointing to s.HomeID.
0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 39602545
I think Kaufned is correct. The query should be:

From
      s In db.Schedules
      Join t1 hteams In db.Teams
            On s.HomeID Equals t1.Teamid
      Join t2 hteams In db.Teams
            On sAwayID Equals t2.Teamid
Where
      s.Week = 3
      And s.Season = "2014"
Select
      HomeTeamId       =s.HomeID,
      HomeTeam       =t1.TeamName,
      AwayTeamId      =s.AwayId,
      AwayTeam      =t1.TeamName
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39602992
One correction the last line should be:

AwayTeam      =t2.TeamName
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question