In my previous post about using LINQ to SQL in multi layered apps, I mentioned that this query was not optimal because I would need to build the POCOs from the objects returned by LINQ. This would mean that objects would be created twice.
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 };
Not ideal so I asked if anyone had an idea on how to do that. Stefan Sedich suggested this query:
var q = from o in ctx.Orderswhere o.CustomerID == idselect 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};
Cool! The query returns the Order complex object with the Detail property filled from orders Details.
Thanks a lot Stefan!
Remember Me
Theme design by Jelle Druyts
Powered by: newtelligence dasBlog 2.1.8102.813
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. Since I'm self employed, this mean that these do not represent my own opinions. Gee, go figure!No chipmunks were harmed while coding, deploying and configuring this blog...well, maybe a little bit but not that much. Hey, some of them even enjoyed it. Crazy chipmunks ;-)
Warning: While reading this blog, do not operate heavy machinery, never use a lit match or open flame to check fuel level, don�t try to dry yourself in a microwave oven, do not play in the dishwasher, do not use this blog in any function that involves insertion into a body cavity. Reading in conjunction with alcohol or sedatives may cause drowsiness. May irritate eyes. Use before the expiration date. If you do not understand, or cannot read all directions, cautions and warnings, do not read this blog. Warning - Contents may be hot and under pressure. May blow off causing eye or other serious injury. Point away from face and people, especially while reading. Suitable for vegetarians. This is not a dating agency. Not to be used for anything else. Reading this blog does not enable you to fly. CAUTION: Risk of electric shock - Dot not open. Do not use as an ice cream topping. Not for intimate hygiene.
© Copyright 2009, Guy Barrette
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
E-mail