Question

When working with Linq to SQL you can use DataLoadOptions to specify which "child" objects to load. Is there a similar technique with BLToolkit?

It's nice that with BLT I can create the BO directly, like:

from p in db.Parent
select new Parent
{
  ParentId = p.ParentId,
  Child = p.Child
};

however going this route, while the entire Child object is created, I would need to specify every field in Parent (i.e. ParentId, ParentName, ParentDob, etc.)

Thanks.

Was it helpful?

Solution

Not exactly like LoadWith, but in my opinion the approach below is even better / cleaner (less magic). In your BO make a static function that would look like this:

public static Parent Build(Parent parent, Child child)
{
  parent.Child = child;
  return parent; 
}

Now you'd need to write your LINQ query like this:

var query = from p in db.Parent
            select Parent.Build(p, p.Child);

So rather than "select p" or "select new Parent()" we let the static function return "p" but also assign the Child object to "parent.Child" before returning. As long as your associations are setup properly (BLToolkit.Mapping.Association) p.Child would tell BLT to join to the Child table as well. You could go even further, i.e. p.Child.Friends.etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top