Skip to main content

Posts

Showing posts from October, 2011

LINQ to Object JOIN

I was reviewing a piece of LINQ to Object Join query of one of my colleagues and a code that is written like this (example, not the exact code, the actual query was complex then this…)... IList < int > result = ( from i in col1 join j in col2 on i equals j select i).ToList(); told him to re-write it like this…or using a 2 foreach loop, as the join was too complex. IList < int > result = ( from i in col1 from j in col2 where i == j select i).ToList(); my Colleague, argued that Microsoft would have implemented the JOIN operator in a better way then a 2 loops running on each other… I ran a few test on the 2 queries above, and the query that used the JOIN operator was way faster… The reason is that the JOIN operator (the extension method) is implemented such that the outer collection’s key is enumerated once and put into a hash table, next the inner collection is iterated and it’s key is checked to see if it’s there in the hashtable, this is just O(1) operati