2009-01-26 74 views
15

Select和SelectMany更喜歡加入嗎?LinqToSQL Select and SelectMany與加入

我不知道的原因是因爲我使用LinqPad並在一節有評論認爲說:

// Note: before delving into this section, make sure you've read the preceding two 
// sections: Select and SelectMany. The Join operators are actually unnecessary 
// in LINQ to SQL, and the equivalent of SQL inner and outer joins is most easily 
// achieved in LINQ to SQL using Select/SelectMany and subqueries! 

然而,在其他部分也清楚地表明,連接速度更快(至少爲例子在LinqPad中給出)和對我來說,他們更容易在我的腦海中形象化。

也許我誤解了,因爲我只查看代碼示例而不是書,但是我看到其他人推薦Select和SelectMany over Joins。

回答

15

有關此事的Wayward博客有this to say。 Join使用一組顯式的參數來加入特定的鍵,並允許左外連接和右外連接。 SelectMany執行monadic bind,通常會導致內連接或交叉連接。由於LINQ本質上是.NET中函數式編程的實現,因此SelectMany更爲自然。然而,Join的顯式設置可能會導致更快的操作。

至於首選,我認爲無論什麼讀得最清楚給你是最好的。 C# version of 101 LINQ Samples不包括加入,但VB list顯示在不同情況下使用的加入和SelectMany。

+0

謝謝,很好的回覆和鏈接。從非答覆的角度來看,我認爲應該使用哪一個並沒有太大的差別。 – dtc 2009-02-12 23:56:06