I’m by far not a LINQ master, but I’m getting into some complex queries now that I’ve rolled my Data Access Layer into Entity Framework 4. The following is the SQL I have converted into LINQ
select DISTINCT DegreeCategories.CategoryTitle
from DegreeCategories
inner join Degrees on DegreeCategories.DegreeCategoryID = Degrees.DegreeCategoryDegreeCategoryID
inner join Programs on Programs.DegreesDegreeID = Degrees.DegreeID
inner join ProgramCategories on Programs.ProgramCategoriesCategoryID = ProgramCategories.CategoryID
inner join OccuPathBridge on OccuPathBridge.ProgramCategoryID = ProgramCategories.CategoryID
inner join CareerMap on OccuPathBridge.OccupationID = CareerMap.OccupationID
where Programs.DegreesDegreeID in (
select Degrees.DegreeID from Degrees where Programs.ProgramCategoriesCategoryID in
( select ProgramCategories.CategoryID from ProgramCategories where CategoryID in
( select OccuPathBridge.ProgramCategoryID from OccuPathBridge where OccuPathBridge.OccupationID in
(select OccupationID from CareerMap where CareerMap.OccupationTitle = 'Pharmacists')
)
)
)
The Linq as best I can tell is 1:1 – however the containing type does not contain a method for “Contains()” which is causing the expression to fail all together.
(from degreecategories in db.DegreeCategories
join degrees in db.Degrees on new { DegreeCategoryID = degreecategories.DegreeCategoryID } equals new { DegreeCategoryID = degrees.DegreeCategoryDegreeCategoryID }
join programs in db.Programs on new { DegreesDegreeID = degrees.DegreeID } equals new { DegreesDegreeID = programs.DegreesDegreeID }
join programcategories in db.ProgramCategories on new { ProgramCategoriesCategoryID = (Int32)programs.ProgramCategoriesCategoryID } equals new { ProgramCategoriesCategoryID = programcategories.CategoryID }
join occupathbridges in db.OccuPathBridges on new { ProgramCategoryID = programcategories.CategoryID } equals new { ProgramCategoryID = (Int32)occupathbridges.ProgramCategoryID }
join careermaps in db.CareerMaps on occupathbridges.OccupationID equals careermaps.OccupationID
where
(from degrees0 in db.Degrees
where
(from programcategories0 in db.ProgramCategories
where
(from occupathbridges0 in db.OccuPathBridges
where
(from careermaps0 in db.CareerMaps
where
careermaps0.OccupationTitle == "Pharmacists"
select new {
careermaps0.OccupationID
}).Contains(new { occupathbridges0.OccupationID })
select new {
occupathbridges0.ProgramCategoryID
}).Contains(new { ProgramCategoryID = (Int32?)programcategories0.CategoryID })
select new {
programcategories0.CategoryID
}).Contains(new { CategoryID = (Int32)programs.ProgramCategoriesCategoryID })
select new {
degrees0.DegreeID
}).Contains(new { programs.DegreesDegreeID })
select new {
degreecategories.CategoryTitle
}).Distinct()
Where do I begin to start translating this query into a Parallel Query?
I’ve included all the requisite includes
using System.Linq;
using System.Data.Entity;
using System.Data.Linq;
using MyProjects.DAL;
Is there something obvious I’m missing? I’ve used Linqer, Linqpad, and several tutorials out there on google to try and write sub-select based queries. none of which have yielded any results.
As just one example of what’s wrong with the SQL version, we have this:
Since the
WHEREclause here isn’t referencing theDegreestable at all, that’s effectively selecting all rows from that table. So, it seems to be a null operation.Can you confirm if the following query gives equivalent results:
Then we can look at converting it to an EF/LINQ query.