LinqToSQL Select和SelectMany vs Join

时间:2009-01-26 19:30:19

标签: .net sql linq linq-to-sql .net-3.5

Select和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而不是联接。

1 个答案:

答案 0 :(得分:15)

Wayward Weblog就此问题this to say。 Join使用一组显式参数来连接特定键,并允许左外连接和右外连接。 SelectMany执行monadic bind,通常会导致内部联接或交叉联接。由于LINQ本质上是.NET中函数式编程的一种实现,因此SelectMany是更自然的表达式;但是,显式设置加入可能会导致更快的操作。

至于首选,我认为对你来说最清楚的是最好的。 C# version of 101 LINQ Samples不包含加入,但VB list显示在不同情况下使用的加入和选择。