开发者

sorting on related field in llblgen 2.6

I inherited an application that uses llblgen 2.6. I have a PersonAppointmentType entity that has a AppointmentType property (n:1 relation). Now I want to sort a collection of PersonAppointmentTypes on the name of the AppointmentType. I tried this so far in the Page_Load:

    if (!Page.IsPostBack)
    {
        var p = new PrefetchPath(EntityType.PersonAppointmentTypeEntity);
        p.Add(PersonAppointmentTypeEntity.PrefetchPathAppointmentType);

        dsItems.PrefetchPathToUse = p;
        // dsItems.SorterToUse = new SortExpression(new SortClause(PersonAppointmentTyp开发者_运维百科eFields.StartDate, SortOperator.Ascending));  // This works
        dsItems.SorterToUse = new SortExpression(new SortClause(AppointmentTypeFields.Name, SortOperator.Ascending));
    }

I'm probably just not getting it.

EDIT: Phil put me on the right track, this works:

    if (!Page.IsPostBack)
    {
        dsItems.RelationsToUse = new RelationCollection(PersonAppointmentTypeEntity.Relations.AppointmentTypeEntityUsingAppointmentTypeId);
        dsItems.SorterToUse = new SortExpression(new SortClause(AppointmentTypeFields.Name, SortOperator.Ascending));
    }


You'll need to share more code if you want an exact solution. You didn't post the code where you actually fetch the entity (or collection). This may not seem relevant but it (probably) is, as I'm guessing you are making a common mistake that people make with prefetch paths when they are first trying to sort or filter on a related entity.

You have a prefetch path from PersonAppointmentType (PAT) to AppointType (AT). This basically tells the framework to fetch PATs as one query, then after that query completes, to fetch ATs based on the results of the PAT query. LLBLGen takes care of all of this for you, and wires the objects together once the queries have completed.

What you are trying to do is sort the first query by the entity you are fetching in the second query. If you think in SQL terms, you need a join from PAT=>AT in the first query. To achieve this, you need to add a relation (join) via a RelationPredicateBucket and pass that as part of your fetch call.

It may seem counter-intuitive at first, but relations and prefetch paths are completely unrelated (although you can use them together). You may not even need the prefetch path at all; It may be that you ONLY need the relation and sort clause added to your fetch code (depending on whether you actually want the AT Entity in your graph, vs. the ability to sort by its fields).

There is a very good explanation of Prefetch Paths and how they were here:

http://www.llblgening.com/archive/2009/10/prefetchpaths-in-depth/

Post the remainder of your fetch code and I may be able to give you a more exact answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜