Inner joins in LINQ
2009-Сер-12, Середа 10:30I always get confused when I need to write inner join in LINQ.
Yesterday I found good article about it.
I short, solution is below. Details are there.
Each flight has two Hubs, one arrival and the other one is the departure.
The query has to get the available flights for the selected arrival
and departure directorates.
In T-SQL, it can be written as following:
SELECT f.*
FROM flights f
INNER JOIN Hubs sh
ON f.HubFrom = sh.HubId
INNER JOIN CityHubs sch
ON sh.HubId = sch.HubId
INNER JOIN Cities sc
ON sch.CityId = sc.CityId
INNER JOIN Directorates sd
ON sc.CityId = sd.CityId
INNER JOIN Hubs dh
ON f.HubTo = dh.HubId
INNER JOIN CityHubs dch
ON dh.HubId = dch.HubId
INNER JOIN Cities dc
ON dch.CityId = dc.CityId
INNER JOIN Directorates dd
ON dd.CityId = dc.CityId
WHERE sd.DirectorateID = @sourceDirectorateID
AND dd.DirectorateId = @destinationDirectorateID
In LINQ, it is written as following:
from f in db.Flights
from fc in f.Hubs.Cities
from fd in fc.Directorates
from tc in f.Hubs1.Cities
from td in tc.Directorates
where fd.DirectorateID == sourceDirectorateID
&& td.DirectorateID == destinationDirectorateID
select f;