lazyPower
lazyPower

Reputation: 267

Convert this expression from Linq.IQueryable to Linq.ParalellQuery

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.

Upvotes: 1

Views: 279

Answers (2)

aalicagan
aalicagan

Reputation: 65

Maybe you can write db.DegreeCategories.AsEnumerable()

Upvotes: 0

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239814

As just one example of what's wrong with the SQL version, we have this:

 in ( 
        select Degrees.DegreeID from Degrees where Programs.ProgramCategoriesCategoryID in

Since the WHERE clause here isn't referencing the Degrees table 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:

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 CareerMap.OccupationTitle = 'Pharmacists'

Then we can look at converting it to an EF/LINQ query.

Upvotes: 2

Related Questions