IPrefetchPath2 and WithPath

Posts   
 
    
swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 18-Jun-2008 14:34:08   

I have an entity, Article, which as multiple related tables, WrittenByUserEntity, SectionEntity, TechnologyEntity. In non-Linq I would create an IPrefetchPath2, and .Add the ArticleEntity's many PrefetchPaths.

Using WithPath I find examples in the unittests and documentation of doing things like customer->order->orderdetail, but I can't seem to figure out how to add multiple prefetch-style items to a single entity type, like so:

Article->WrittenByUser Article->Section(s) Article->Technology(s)

I'm around this temporarily by simply building the IPrefetchPath2, adding the Prefetchs, and using .WithPath(mPrefetchs), which is cool, but not very Linq like.

What's the trick here?

swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 18-Jun-2008 14:36:59   

I guess an example always helps:

SortExpression mOrderBySections = new SortExpression(ArticleSectionFields.Sectionorder | SortOperator.Ascending);
SortExpression mOrderByLanguages = new SortExpression(LanguageFields.Name | SortOperator.Ascending);
SortExpression mOrderByTechnology = new SortExpression(TechnologyFields.Name | SortOperator.Ascending);

IPrefetchPath2 mPrefetch = new PrefetchPath2((int)EntityType.ArticleEntity);
mPrefetch.Add(ArticleEntity.PrefetchPathWrittenByUser);
mPrefetch.Add(ArticleEntity.PrefetchPathArticleSections, 0, null, null, mOrderBySections);
mPrefetch.Add(ArticleEntity.PrefetchPathLanguages, 0, null, null, mOrderByLanguages);
mPrefetch.Add(ArticleEntity.PrefetchPathTechnologies, 0, null, null, mOrderByTechnology);

LinqMetaData metaData = new LinqMetaData(mAdapter);

var q = (from a in metaData.Article
         select a)
        .WithPath(mPrefetch);
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Jun-2008 16:36:11   

From the docs:

The following example illustrates this. This example fetches all Customer entities from Germany, their Orders, per Order the OrderDetail instances and also the Employee entities related to the Order entities fetched. The PathEdge instance for Order therefore has two PathEdge instances below it: one for OrderDetail and one for Employee. Both are specified in the constructor call of the PathEdge of Order.

var q = (from c in metaData.Customer where c.Country=="Germany" select c) .WithPath( new PathEdge<OrderEntity>( CustomerEntity.PrefetchPathOrders, new PathEdge<OrderDetailsEntity>(OrderEntity.PrefetchPathOrderDetails), new PathEdge<EmployeeEntity>(OrderEntity.PrefetchPathEmployee)));

swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 18-Jun-2008 17:29:27   

I've seen that portion of the docs, which is duplicated in the unittests, but it doesn't cover the examples I've described.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Jun-2008 18:17:39   

swallace wrote:

I've seen that portion of the docs, which is duplicated in the unittests, but it doesn't cover the examples I've described.

Yes, it actually does wink

You see two pathedge creations here:

                     CustomerEntity.PrefetchPathOrders,
                     new PathEdge<OrderDetailsEntity>(OrderEntity.PrefetchPathOrderDetails),
                     new PathEdge<EmployeeEntity>(OrderEntity.PrefetchPathEmployee)));

these are the two subnodes of Orders. (so path.Add(node))

You can do the same thing with WithPath: .WithPath( new PathEdge(_a), new PathEdge(_b), new PathEdge(_c_), ...

which is the same as: path.Add(_a) path.Add(_b) path.Add(_c_)

Frans Bouma | Lead developer LLBLGen Pro
swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 18-Jun-2008 21:06:32   

Sure enough, I missed that. I'll give it a try.

swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 19-Jun-2008 00:03:21   

Yes, that did it. I'm adding the final code here for completeness:

SortExpression mOrderBySections = new SortExpression(ArticleSectionFields.Sectionorder | SortOperator.Ascending);
SortExpression mOrderByLanguages = new SortExpression(LanguageFields.Name | SortOperator.Ascending);
SortExpression mOrderByTechnology = new SortExpression(TechnologyFields.Name | SortOperator.Ascending);

LinqMetaData metaData = new LinqMetaData(mAdapter);

var q = (from a in metaData.Article
         select a)
        .WithPath(
            new PathEdge<ArticleSectionEntity>(ArticleEntity.PrefetchPathArticleSections, null, mOrderBySections, 0),
            new PathEdge<LanguageEntity>(ArticleEntity.PrefetchPathLanguages, null, mOrderByLanguages, 0),
            new PathEdge<TechnologyEntity>(ArticleEntity.PrefetchPathTechnologies, null, mOrderByTechnology, 0),
            new PathEdge<UserEntity>(ArticleEntity.PrefetchPathWrittenByUser));

EntityCollection<ArticleEntity> mArticles = ((ILLBLGenProQuery)q).Execute<EntityCollection<ArticleEntity>>();

Very Linq-y!