【问题标题】:LinqToSQL Select and SelectMany vs Join [closed]LinqToSQL Select 和 SelectMany vs Join [关闭]
【发布时间】:2010-10-03 14:53:45
【问题描述】:

Select 和 SelectMany 是否比 Joins 更可取?

我想知道的原因是因为我使用 LinqPad,并且在一个部分中有 cmets 说:

// 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 而不是 Joins。

【问题讨论】:

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


    【解决方案1】:

    任性的博客有this to say关于此事。 Join 使用一组明确的参数来连接特定的键,并允许左外连接和右外连接。 SelectMany 执行monadic bind,通常会导致内部连接或交叉连接。由于 LINQ 本质上是 .NET 中函数式编程的实现,因此 SelectMany 是更自然的表达方式;但是,显式设置 Join 可能会导致更快的操作。

    至于首选,我认为对你来说读得最清楚的就是最好的。 C# version of 101 LINQ Samples 不包括 Join,但 VB list 显示不同场景中使用的 Join 和 SelectMany。

    【讨论】:

    • 谢谢,很好的回复和链接。从未回复的情况来看,我认为应该使用哪个确实没有太大差异。
    猜你喜欢
    • 2011-05-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多