2012-01-27 21 views
1

我到目前爲止還不是LINQ主控,但現在我已經將我的數據訪問層轉換爲實體框架4中的一些複雜查詢。在SQL我已轉換成LINQ將此表達式從Linq.IQueryable轉換爲Linq.ParalellQuery

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 - 然而包含類型不包含的方法「包含()」,這是使表達失敗所有一起。

(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和幾個教程來嘗試編寫基於子選擇的查詢。沒有一個產生任何結果。

+1

在轉換之前重新編寫SQL查詢有點理智嗎?很多這些連接似乎都是不必要的(提示:如果在查詢中有'DISTINCT',那麼你錯誤的寫入機會大於80%] – 2012-01-27 07:59:57

+0

這些數據庫表的顯示方式都是如此。我認爲數據庫的規範化並沒有從2NF中解脫出來。我不得不遍歷所有這些表格以獲取我想要的數據,因爲職業頭銜和學位類別之間沒有直接聯繫。雖然我意識到這是糟糕的形式 - 它是我必須運作的參數。在一個完美的世界和所有......我會重新設計表格。 – lazyPower 2012-01-27 08:07:26

回答

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查詢。

+0

你先生,你做得對。這給了我完全相同的結果。 – lazyPower 2012-01-27 08:09:51

+0

@lazyPower - 希望你可以在沒有我們的幫助的情況下轉換爲linq。我CWed這個「答案」,因爲它不是你提出的問題的實際答案(但我需要更多的格式化空間而不是評論)。 – 2012-01-27 08:12:53

+0

哭泣,你的解決方案比我所要求的更優雅。當我做錯了的時候,我一直在尋找。 – lazyPower 2012-01-27 08:15:15

0

也許你可以寫db.DegreeCategories.AsEnumerable()