2011-12-13 27 views
0

這是我第一次嘗試通過linq進行左連接,我已經通過堆棧溢出和谷歌瀏覽過,但是我試過的所有東西都沒有工作(很可能是因爲我自己缺乏理解)。我嘗試做以下查詢:LINQ到SQL試圖留下DefaultIfEmpty加入()導致用於查詢操作「DefaultIfEmpty」不支持過載。錯誤

IQueryable<MyType> pQ = (from prd in dc.ProductDatas 
join cc in dc.CategoryProducts.DefaultIfEmpty(defaultCP) 
on prd.ProductID equals cc.ProductID 
where cc.CatID == CatID 
orderby cc.OrdWithinInCategory 
select prd); 

我已經defind defaultCP爲:

CategoryProduct defaultCP = new CategoryProduct {ID = 1,CatID = CatID, OrdWithinInCategory = 999}; 

我收到以下錯誤:

用於查詢操作 'DefaultIfEmpty' 不支持過載。

描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.NotSupportedException:用於查詢運算符'DefaultIfEmpty'的不受支持的重載。

有沒有什麼明顯的我在做我的代碼錯了,還是我需要嘗試完全不同的方法。任何幫助非常感謝。

+0

您的'where'子句將消除左連接的影響,因爲具有null的cc.CatID行將被刪除。你的「被」條款是否正確?系統抱怨'defaultCP'。用簡單的'DefaultIfEmpty()'重載,你看到的錯誤應該消失了(儘管你可能會得到一個新的錯誤)。 – dasblinkenlight

+0

是否指定了默認的CP的CatID作爲CatID停止被刪除的行,因爲它們將等於我檢查的值? – John

+0

它在內存中執行,但據我所知,SQL中沒有相應的構造。 – dasblinkenlight

回答

0

使用AsEnumerable()。

CategoryProduct defaultCP = new CategoryProduct {ID = 1,CatID = CatID, OrdWithinInCategory = 999}; 

IQueryable<MyType> pQ = (from prd in dc.ProductDatas 
         join cc in dc.CategoryProducts.DefaultIfEmpty(defaultCP) 
          on prd.ProductID equals cc.ProductID 
         where cc.CatID == CatID 
         orderby cc.OrdWithinInCategory 
         select prd) 
       .AsEnumerable() 
       .DefaultIfEmpty(defaultCP).First();