Print Page | Close Window

Include with Inheritance

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2014
Printed Date: 14-Mar-2025 at 12:51pm


Topic: Include with Inheritance
Posted By: rhaney
Subject: Include with Inheritance
Date Posted: 27-Jul-2010 at 3:29pm

Let's say I have an e-commerce site, with different types of products.  For instance, books and dvds.  I create a model with the following entities:

Catalog
Product
Book (of type Product)
Dvd (of type Product)
Author
Director
 
Catalog has Products (Books/Dvds)
Book has a collection of Authors
Dvd has a single Director
 
How would I eager load information about Books and Dvds in the same query?
 
I tried the following, but get an exception:
var query = from c in em.Catalogs.AddIncludePaths("Products, "Products.Authors", "Products.Director");
 
I was thinking a Silverlight like syntax might be nice:
var query = from c in em.Catalogs.AddIncludePaths("Products, "Products.(Book.Authors)", "Products.(Dvd.Director)");
 
Does such a syntax exist?



Replies:
Posted By: ting
Date Posted: 27-Jul-2010 at 5:42pm
Good question.  The .Include functionality in LINQ (which we mirror) does not support this, but you can do this by using a projection in DevForce.  I believe the query below should accomplish what you were trying to do.  It returns an anonymous type, but all of the various components will still be brought into the DevForce cache.
 
var results = em.Catalogs.Select(c => new {
  Catalog = c,
  Products = c.Products,
  Authors = c.Products.OfType<Book>().Select(b => b.Author),
  Directors = c.Products.OfType<Dvd>().Select(d => d.Director)
 }).Execute();
 


Posted By: rhaney
Date Posted: 27-Jul-2010 at 5:48pm
That's a great suggestion :-)....in fact, I like it a lot better than the Include statements....(strongly typed, deep eager loading...)
 
Will try it out....thanks!



Print Page | Close Window