LINQ to SQL: Returning Complex Objects (Performance Problems?)
In my previous post, one reader commented that the proposed LINQ query would be utterly slow so I did a quick unscientific showdown.
In the left corner: the query returning complex objects from
anonymous types:
var q = from o in ctx.Orders
where
o.CustomerID == id
select new { Detail =
o.Order_Details, CustomerID = o.CustomerID, OrderDate =
o.OrderDate, OrderID = o.OrderID, ShippedDate =
o.ShippedDate, ShipCity = o.ShipCity };
In the right corner: the query returning complex objects
from POCOs:
var q = from o in ctx.Orders
where o.CustomerID ==
id
select new TransportObjects.Northwind.Order {
Detail = o.Order_Details.Select(item => new
TransportObjects.Northwind.OrderDetail {
ProductID =
item.ProductID
}).ToArray(),
CustomerID =
o.CustomerID, OrderDate = o.OrderDate, OrderID =
o.OrderID, ShippedDate = o.ShippedDate, ShipCity =
o.ShipCity};
The weapons: 50,000 orders each having 10 order details rows meaning 500,000 objects.
The result: it's a tie!
Yep, both queries returned the results in about 2 seconds using SQL Server Express 2005 locally (no layers, no WCF etc)