orderby in prefetch error

Posts   
 
    
leon
User
Posts: 9
Joined: 23-Dec-2010
# Posted on: 11-Jul-2011 17:41:15   

Hi,

I want to get one specific "Parent" from a table, and his "children". The children must be ordered by "year". The next statement gives a runtime exception: "Expression must be a MemberExpression"

 ParentEntity myParent = metaData.Parent
      .Where(p => p.Id == Id)
      .WithPath(  gp => gp.Prefetch(o => o.Children.OrderBy(ob => ob.Year)  ))
      .First<ParentEntity>();

I can't see anything wrong ?

Leon

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Jul-2011 18:02:38   

The PrefetchPath should be applied on an IQueryable, so "select" before adding the prefetchPath. As in here:

var q = (from c in metaData.Customer
         where c.Country == "Germany"
         select c).WithPath(...
leon
User
Posts: 9
Joined: 23-Dec-2010
# Posted on: 11-Jul-2011 19:33:09   

Walaa,

Using of generic prefetch<ChildEntity> did the trick

ParentEntity parent = metaData.Parent
                .Where(p => p.Id == Id)
                .WithPath(gp => gp.Prefetch<ChildEntity>(o => o.Children)
                                  .OrderBy(ob => ob.AanlegJaar)
                               )
                .First<ParentEntity>();

Greatings

Leon wink