views:

1376

answers:

1

Are Select and SelectMany preferrable to Joins?

The reason I'm wondering is because I use LinqPad and in one section there are comments that say:

// 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!

Yet, in other sections it clearly shows that joins are faster (at least for the examples given in LinqPad) and to me they are easier to visualize in my head.

Perhaps I'm misunderstanding since I'm only looking through the code samples and not the book, but I've seen other people recommend Select and SelectMany over Joins as well.

+7  A: 

The Wayward Weblog had this to say regarding the matter. Join uses an explicit set of parameters to join on a specific key and allows for left and right outer joins. SelectMany performs a monadic bind that generally results in either an inner join or cross join. As LINQ is essentially an implementation of functional programming in .NET, the SelectMany is the more natural expression; however, the explicit setup of the Join likely results in faster actions.

As to preferred, I think whatever reads the most clearly to you is best. The C# version of 101 LINQ Samples doesn't include Join, but the VB list shows Join and SelectMany used in different scenarios.

Ryan Riley
Thank you, good reply and link. Judging by the non-replies I assume that there really isn't a huge difference on which should be used.
metanaito