2011-09-20 48 views
0

加入我使用EF4多個左外與集合和EF4

var q = from a in FunctionA 
     from b in FunctionB.Where(a=>a.Id== b.Id).DefaultIfEmpty() 
     from c in FunctionC.Where(c=>c.Id== b.Id).DefaultIfEmpty() 
     select a; 

其中泛函,FunctionB和FunctionC返回集合LINQ的聲明。有些數據條件,我得到一個空的異常,因爲在函數B.Where(a => a.Id == b.Id).DefaultIfEmpty()的b中的b的值有時爲null然後聲明「從FunctionC.Where(c => c.Id == b.Id).DefaultIfEmpty()中的c」爆炸,因爲b爲空。

什麼是做外部正確的方式在這裏加入?請幫忙 !

謝謝!

+1

如果FunctionB與FunctionA和FunctionC獨立於FunctionB,那麼查詢將最有可能在DB中創建兩個CROSS JOINS,並且它將具有可怕的性能。 –

回答

0

Where語法看起來不正確的,我想你實際上是使用linq-to-objects,但所有你需要做的就是添加一個條件檢查。當b不爲空

編輯 - 根據您的意見,你想這樣做

var q = from a in FunctionA 
      from b in FunctionB.Where(x => a.Id == x.Id).DefaultIfEmpty() 
      from c in FunctionC.Where(x => b != null && b.Id== x.Id).DefaultIfEmpty() 
      select a; 
+0

對不起。我打算說哪裏(c => c.Id == b.Id)。我會盡快嘗試你的建議!謝謝 !我可以使用上面顯示的相同語法嗎? – user636525

+0

我無法添加條件Where(b!= null)!!是否有使用不同的語法?謝謝 ! – user636525

+0

@ user636525 - (!X => B =空&& b.Id == x.Id)檢查我的更新,這應該爲你 – Aducci