LinqToSQL Select和SelectMany vs Join

dtc*_*dtc 15 .net sql linq .net-3.5 linq-to-sql

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!
Run Code Online (Sandbox Code Playgroud)

然而,在其他部分中,它清楚地表明连接速度更快(至少对于LinqPad中给出的示例),对我来说,它们更容易在我的脑海中可视化.

也许我误解了,因为我只是查看代码示例而不是书,但我看到其他人也推荐Select和SelectMany而不是Joins.

小智 15

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

至于首选,我认为对你来说最清楚的是最好的.101 LINQ SamplesC#版本不包含Join,但VB列表显示在不同场景中使用的Join和SelectMany.