Solved

LINQ Query Joining same table twice

Posted on 2013-10-25
4
1,227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

763 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