将此表达式从Linq.IQueryable转换为Linq.ParalellQuery

时间:2012-01-27 07:46:40

标签: c# sql linq .net-4.0

我到目前为止还不是LINQ的主人,但是我现在已经进入了一些复杂的查询,因为我已将我的数据访问层转换为实体框架4.以下是我已转换为LINQ的SQL

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')
                    )
                )
            )

Linq尽我所知是1:1 - 但是包含类型不包含“Contains()”的方法,导致表达式一起失败。

(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()

我从哪里开始将此查询转换为并行查询?

我已经包含了所有必需的包含

using System.Linq;
using System.Data.Entity;
using System.Data.Linq;
using MyProjects.DAL;

有什么明显的东西我不见了吗?我在谷歌上使用了Linqer,Linqpad和一些教程来尝试编写基于子选择的查询。没有一个产生任何结果。

2 个答案:

答案 0 :(得分:2)

作为 SQL 版本出现问题的一个例子,我们有:

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

由于此处的WHERE子句根本没有引用Degrees表,因此可以有效地从该表中选择所有行。所以,它似乎是一个空操作。

您能否确认以下查询是否给出了相同的结果:

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'

然后我们可以看一下将其转换为EF / LINQ查询。

答案 1 :(得分:0)

也许你可以写db.DegreeCategories.AsEnumerable()