I\'ve recently upgraded to VS 2010 and am playing around with LINQ to Dataset. I have a strong typed dataset for Authorization that is in HttpCache of an ASP.NET WebApplicat
what you really need to know is the sql that was created for the two statements. There are a few ways of getting to it but the simplest is to use LinqPad. There are several buttons right above the query results that will change to the sql. That will give you a lot more information than anything else.
Great information you shared there though.
The Join
is much faster, because the method knows how to combine the tables to reduce the result to the relevant combinations. When you use Where
to specify the relation, it has to create every possible combination, and then test the condition to see which combinations are relevant.
The Join
method can set up a hash table to use as an index to quicky zip two tables together, while the Where
method runs after all the combinations are already created, so it can't use any tricks to reduce the combinations beforehand.
Your first approach (SQL query in the DB) is quite efficient because the DB knows how to perform a join. But it doesn't really make sense to compare it with the other approaches, since they work directly in memory (Linq to DataSet)
The query with multiple tables and a Where
condition actually performs a cartesian product of all the tables, then filters the rows that satisfy the condition. This means the Where
condition is evaluated for each combination of rows (n1 * n2 * n3 * n4)
The Join
operator takes the rows from the first tables, then takes only the rows with a matching key from the second table, then only the rows with a matching key from the third table, and so on. This is much more efficient, because it doesn't need to perform as many operations