Sorting by SubPath

Posts   
 
    
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 22-Feb-2009 15:24:30   

Hi there, got the following table structure:

FontPalette <- FontFamily <- FontFamilyTranslation

now I'd like to sort the FontFamilies by the Name property of the FontFamily-Translation. I dont want to sort the FontFamily-Translation entries. Here's my PrefetchPath:


    public static PathEdge<FontPaletteEntity>[] FontPalettePrefetch = {
      new PathEdge<FontPaletteEntity>(FontPaletteEntity.PrefetchPathFontFamily, null, 
        new SortExpression(FontFamilyTranslationFields.Name | SortOperator.Ascending), 0,
                                      new PathEdge<FontFamilyEntity>(
                                        FontFamilyEntity.PrefetchPathFontFamilyTranslation))
    };

Somehow the relation between FontFamilyTranslation -> FontFamily is not being recovered while trying to assert the sort clause. I know I do had to specify the relation using non-Linq. Do I have to specify it using Linq too, and if so, how? The error message is exactly the same I get when leaving the relation of the non-Linq query. I'm using the latest build of the libs.

Regards, André

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Feb-2009 19:45:09   

In that prefetchPath code you are sorting the subPath. You need to put the sort clause on the main query.

David Elizondo | LLBLGen Support Team
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 22-Feb-2009 20:19:26   

Hi David,

I'm using the FontPalettePrefetch(part of SystemManager) member in another query... here's the full query.


using (var adapter = new DataAccessAdapter()) {
        var metaData = new LinqMetaData(adapter);
        IQueryable<TemplateEntity> q =
          (from t in metaData.Template
           where t.Id.Equals(id)
           select t).WithPath(
            new PathEdge<TemplateEntity>(TemplateEntity.PrefetchPathTemplateTranslation),
            new PathEdge<TemplateEntity>(TemplateEntity.PrefetchPathTemplateFontPalette,
                                         new PathEdge<TemplateFontPaletteEntity>(
                                           TemplateFontPaletteEntity.PrefetchPathFontPalette,
                                           SystemManager.FontPalettePrefetch))
            );
}

by placing the SortExpression in the actual linq select wouldn't that mean that the TemplateEntitites get sorted not the FontFamily entities? I want to sort the FontFamilies by their FontFamilyTranslation(using the Name property). Where exactly do I have to place the sort clause?

Regards, André

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Feb-2009 06:23:31   

I know what you mean now simple_smile

You want to do something like this:

Goal Fetch one _Customer _and its related _Orders _(prefetch), ordered by Order.Employee.FirstName, additionally fetch each associated employee of each order.

PreidicateExpression code

IPrefetchPath2 path = new PrefetchPath2((int)EntityType.CustomerEntity);
path.Add(CustomerEntity.PrefetchPathOrders, 0, null,
    new RelationCollection(OrderEntity.Relations.EmployeeEntityUsingEmployeeId),
    new SortExpression(EmployeeFields.FirstName | SortOperator.Ascending))
    
    .SubPath.Add(OrderEntity.PrefetchPathEmployee);

PathEdges code I didn't find a way for doing this with PathEdges (will look into it).

Lambda expressions code (using OrderBy method)

var q = (from c in metaData.Customer
         where c.CustomerId == "ALFKI"
         select c)
        .WithPath<CustomerEntity>(
            p => p.Prefetch<OrderEntity>(c => c.Orders)
                .OrderBy(o => o.Employee.FirstName)

            .SubPath( p2 => p2.Prefetch<EmployeeEntity>(o => o.Employee)));
David Elizondo | LLBLGen Support Team
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 23-Feb-2009 16:04:33   

Hi David,

thats exactly what I was trying to achieve. However, I got all of my prefetches outlined using PathEdges, since, at least for me, it looks better and I'm not really used to the lambda stuff yet. So I'd prefer to do it the PathEdge-way, also because I got a big pile of code written so far. As for now I'm sorting the data-transport objects after the entitites are being fetched, but I'd really like to have it sorted when it comes out of the database.

Regards, André

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Feb-2009 11:25:42   

It's indeed an oversight: a pathedge doesn't have a relations collection to store relations for the sorting. For the filtering, one could use a lambda and it will work, but for sorting that's another story.

The lambda approach uses a collection of sortclauseexpression instances which you in theory could create manually and use with pathedge instances, though that would be very cumbersome. So in short: I'm sorry, we should have added that relations collection. Adding it now is not really possible, as it will likely have a big impact on things, so we'll add it in v3. For you to get forward, please look at the lambda stuff instead. It will be rather straight forward with WithPath, once you've written a couple of queries with it.

Frans Bouma | Lead developer LLBLGen Pro
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 24-Feb-2009 20:22:07   

Hi Frans, thanks for the clarification. Sorting the data on the client-side wont break my app, I can live with that solution. Otherwise I have to use the Lambda prefetching approach.

Keep up the good work!

Regards, André