BLToolkit equivalent of LoadWith from L2S

443 views Asked by At

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.

1

There are 1 answers

0
pbz On BEST ANSWER

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.