Dot-Net

LinqToSQL Select 和 SelectMany 與 Join

  • February 4, 2009

Select 和 SelectMany 是否比 Joins 更可取?

我想知道的原因是因為我使用 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。

任性的部落格對此事發表了這樣的看法。Join 使用一組明確的參數來連接特定的鍵,並允許左外連接和右外連接。SelectMany 執行單子綁定,通常會導致內部連接或交叉連接。由於 LINQ 本質上是 .NET 中函式式程式的實現,因此 SelectMany 是更自然的表達方式;但是,Join 的顯式設置可能會導致更快的操作。

至於首選,我認為對你來說讀得最清楚的就是最好的。101 LINQ Samples的C# 版本不包含 Join,但VB 列表顯示了不同場景中使用的 Join 和 SelectMany。

引用自:https://stackoverflow.com/questions/481023