I wrote quite a lot of code that uses the Linq2Sql-related tables provided to me, only having foreign keys in my database. But, it turns out to be a little time consuming to mock the data for my unit tests. I have to manually establish any relationship in my test harness.
So, I wonder if Linq joins, rather than relying on relationships, will give me a more easily verifiable and possibly more efficient code.
var query = from orderItem in data.OrderItems select new { orderItem.Order.Reference, orderItem.SKU, orderItem.Quantity, }; Console.WriteLine("Relationship Method"); query.ToList().ForEach(x => Console.WriteLine(string.Format("Reference = {0}, {1} x {2}", x.Reference, x.Quantity, x.SKU))); var query2 = from orderItem in data.OrderItems join order in data.Orders on orderItem.OrderID equals order.OrderID select new { order.Reference, orderItem.SKU, orderItem.Quantity, }; Console.WriteLine(); Console.WriteLine("Join Method"); query2.ToList().ForEach(x => Console.WriteLine(string.Format("Reference = {0}, {1} x {2}", x.Reference, x.Quantity, x.SKU)));
Both queries above give me the same result, but is one better than the other in terms of performance and in terms of validation?
source share