One difference I come across is differences in grouping.
When you group linq into objects, you get the result in the form of a hierarchy (keys with child objects).
When you group in SQL, you only get keys and aggregates.
When you group in linq in sql, if you request children (more than aggregates), linq to sql will re-query each group with a key to get these children. If you have thousands of groups, it can be thousands of rounds.
//this is ok var results = db.Orders .GroupBy( o => o.CustomerID ) .Select(g => new { CustomerId = g.Key, OrderCount = g.Count() }); //this could be a lot of round trips. var results = db.Orders .GroupBy( o => o.CustomerID ) .Select(g => new { CustomerId = g.Key, OrderIds = g.Select(o => o.OrderId) }); // this is ok // used ToList to separate linqtosql work from linqtoObject work var results = db.Orders .Select(o => new {o.CustomerId, o.OrderId}) .ToList() .GroupBy(o => o.CustomerId) .Select(g => new { CustomerId = g.Key, OrderIds = g.Select(o => o.OrderId) });
Amy B Nov 12 '09 at 4:00 p.m. 2009-11-12 16:00
source share