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

本文关键字:Linq 转换 ParalellQuery Iquery 表达式 | 更新日期: 2023-09-27 18:21:49

到目前为止,我还不是LINQ大师,但现在我已经将数据访问层集成到Entity Framework 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和谷歌上的一些教程来尝试编写基于子选择的查询。这些都没有产生任何结果。

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

作为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查询。

也许您可以编写db.DegreeCategories.AsEnumerable()