Prefetching based on parent column where many-many involved

Posts   
 
    
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 25-Mar-2013 10:11:12   

(repost updated)

Imagine I have (pseudo tables)

A
{
    Id,
    SomeDate Date
}

B
{
    A.Id,
    C.Id
}

C
{
    Id
}

D
{
    C.Id,
    StartDate Date,
    EndDate Date,
}

A and C is many to many relation through B, C and D is one to many relation.

Now I'd like to fetch A and prefetch D, like pseudo


A.WithPath(aPath => aPath
  .Prefetch<CEntity>(a => a.CCollectionViaB)
     .SubPath(cp => cp
        .Prefetch<DEntity>(c => c.D)
              .FilterOn(only D where A.SomeDate between D.StartDate and D.EndDate)
)

The problem is FilterOn where I can't reach A.SomeDate.

v3.5 17.1.2013

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Mar-2013 20:24:47   

I think you are trying to filter a prefetch query as if it was a joined query. That's not how prefetchs are meant to be used. These queries are not joined with the root entity.

To achieve what you want you will need to treat it as a separate quer, i.e. join with A and use fields from A in filtering.

mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 25-Mar-2013 20:35:52   

Walaa wrote:

I think you are trying to filter a prefetch query as if it was a joined query. That's not how prefetchs are meant to be used. These queries are not joined with the root entity.

To achieve what you want you will need to treat it as a separate quer, i.e. join with A and use fields from A in filtering.

Yep, that's what I could do but then I'd loose entities. But yes, that's a possible way.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Mar-2013 06:32:25   

In theory, another way would be:

A.WithPath(aPath => aPath
.Prefetch<CEntity>(a => a.CCollectionViaB)
     .SubPath(cp => cp
        .Prefetch<DEntity>(c => c.D)
             .FilterOn(d => d.C.Bs.Where(b => b.A.SomeDate >= d.StartDate 
                                                              && b.A.SomeDate <= d.EndDate).Count() > 0
                                            
                          )
      )

I used it here: http://www.llblgening.com/archive/2009/10/prefetchpaths-in-depth/#filteringandsorting In my example It's just two levels and I used discrete values, but it should work.

David Elizondo | LLBLGen Support Team
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 26-Mar-2013 09:18:59   

Hi dealmo,

Yep, that does the trick. I could have thought of that before flushed I greatly improved it though by substituting Count() > 0 with Any() smile