LINQ Query Joining same table twice

Posted on 2013-10-25
Medium Priority
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
Question by:Break40
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
LVL 27

Assisted Solution

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

      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
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.
LVL 27

Accepted Solution

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

      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
      s.Week = 3
      And s.Season = "2014"
      HomeTeamId       =s.HomeID,
      HomeTeam       =t1.TeamName,
      AwayTeamId      =s.AwayId,
      AwayTeam      =t1.TeamName
LVL 27

Expert Comment

ID: 39602992
One correction the last line should be:

AwayTeam      =t2.TeamName

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

770 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