When to prefer joins expressed with SelectMany() over joins expressed with the join keyword in Linq

后端 未结 2 773
误落风尘
误落风尘 2021-01-12 06:08

Linq allows to express inner joins by using the join keyword or by using SelectMany() (i.e. a couple of from keywords) with a where keyword:

var personsToSta         


        
相关标签:
2条回答
  • 2021-01-12 06:28

    For local queries Join is more efficient due to its keyed lookup as Athari mentioned, however for LINQ to SQL (L2S) you'll get more mileage out of SelectMany. In L2S a SelectMany ultimately uses some type of SQL join in the generated SQL depending on your query.

    Take a look at questions 11 & 12 of the LINQ Quiz by Joseph/Ben Albahari, authors of C# 4.0 In a Nutshell. They show samples of different types of joins and they state:

    With LINQ to SQL, SelectMany-based joins are the most flexible, and can perform both equi and non-equi joins. Throw in DefaultIfEmpty, and you can do left outer joins as well!

    In addition, Matt Warren has a detailed blog post on this topic as it pertains to IQueryable / SQL here: LINQ: Building an IQueryable provider - Part VII.

    Back to your question of which to use, you should use whichever query is more readable and allows you to easily express yourself and construct your end goal clearly. Performance shouldn't be an initial concern unless you are dealing with large collections and have profiled both approaches. In L2S you have to consider the flexibility SelectMany offers you depending on the way you need to pair up your data.

    0 讨论(0)
  • 2021-01-12 06:39

    Join is more efficient, it uses Lookup class (a variation of Dictionary with multiple values for a single key) to find matching values.

    0 讨论(0)
提交回复
热议问题