tags:

views:

44

answers:

2

I've got the following sql statement and need to know how to write it in linq, but i can't figure out how to write the multiple inner joins

SELECT Zugehörigkeiten.PK_Dept_ID
FROM mitarbeiter
    INNER JOIN (abteilungen
        INNER JOIN Zugehoerigkeiten
        ON abteilungen.PK_Dept_ID = Zugehoerigkeiten.PK_Dept_ID)
    ON mitarbeiter.MA_ID = Zugehoerigkeiten.MA_ID
WHERE (((Zugehörigkeiten.MA_ID)=1));

Thats what i've got:

var erg = from mitarbeiter
          join abteilungen
          join Zugehoerigkeiten on abteilungen.PK_Dept_ID equals zugehoerigkeiten.PK_Dept_ID on mitarbeiter.MA_ID equals Zugehoerigkeiten.MA_ID
          where zugehoerigkeiten.MA_ID=1
A: 

You're missing a from <some collection> in the linq query

Also, to join on multiple values, you need to create new anonymous objects, because you're only allowed to have a single on <foo> equals <bar>

var erg = from mitarbeiter in mitarbeiters
          join a in abteilungen
          join z in Zugehoerigkeiten 
              on new { a.PK_Dept_ID, mitarbeiter.MA_ID }
              equals new { z.PK_Dept_ID, z.MA_ID }
          where zugehoerigkeiten.MA_ID=1
          select ...

Sometimes it can be much simpler though. See this or this answer.

Sander Rijken
A: 
 var erg = from mitarbeiter in db.Mitarbeiter
      join a in Abteilungen
      join z in Zugehoerigkeiten 
          on new { a.PK_Dept_ID, mitarbeiter.MA_ID }
          equals new { z.PK_Dept_ID, z.MA_ID }
      where zugehoerigkeiten.MA_ID=1
      select z;

I've trie this Code, but it didn't work,...do you have suggestions for me? Im an absolute novice in Linq...

Flow
I think this should've been a comment to my answer?
Sander Rijken
Can you indicate why it didn't work, what error you got, etc? It's fairly hard to come up with a working query based on just SQL
Sander Rijken