Solved

LINQ Query Joining same table twice

Posted on 2013-10-25
4
1,161 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

778 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