Look at the DeepData sample - it has a sub-section that uses LINQ to load the object graph.
The key is to realize that LINQ returns DTOs (data transfer objects), much like you'd get from a web service or WCF service call. You still need to map that data into your business object.
This is really easy in dynamic languages like VB, and somewhat harder in C# - welcome to the new world order The same will likely be true if/when IronPython and IronRuby support LINQ too - all these dynamic languages have a big upside when dealing with LINQ projections.
However, if you use the strongly-typed design tools in VS 2008 things work pretty well in C# too. This is because the tools create a set of DTO classes that look like your database. When you run the LINQ for SQL query you need to avoid using projections, and instead get back data in those DTO types.
Because those types are not dynamic, C# can deal with them easily. You can pass the DTOs through like you would a DataReader from parent to child list to child.
DeepData uses this technique, so it is a valuable example for C#. Just ignore the clever bits where the same code works for DTOs from LINQ or web services interchangeably thanks to the dynamic language features
Check out the blogs for Scott Guthrie and Rick Strahl.
They both discuss LINQ in detail. Scott has all of MS prepping him.
Rick is just diving in and discovering stuff.
I definitely recall reading that you could get LINQ to return a datareader from a query.
So there will be many solutions to this problem in the near future.
I would definitely like to strong type my queries if possible.
Joe
This is the primary catch with LINQ in my view. If you want to
use projections then you must accept the limitations of the dynamic types
created as a result. Or you use tool-created or manually created DTOs. Not much
wiggle room in between…
Rocky
From: Clayton
[mailto:cslanet@lhotka.net]
Sent: Wednesday, August 29, 2007 8:58 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Loading a List with LINQ
I was actually hoping that I could find a way that avoided
DTOs. That feels like I'm almost writing my business objects twice. One to load
the data from the database, and one to do the actual work. I know the DTO can
get away with only have properties and zero business logic, but it somehow
feels redundant to have them.
The DTOs created by the strongly-typed design tools are easy enough for me to
justify because they're what I was wanting to use to strongly-type the
database. The rub comes when I just want to query only a couple columns in one
table and join with a couple of columns in a second table. It just isn't giving
me the small objects I really want.
Copyright (c) Marimer LLC