0
votes

I need to list items with different ItemTypes. These types consist of different parts. Default content query returns items ordered by ID in ascending order. I need to get list in descending order.

IContentQuery<ContentItem>.OrderByDescending<TRecord>() method requires generic parameter (TRecord : ContentPartRecord), which I should use to build ordering creteria. But as I said my content types have different parts.

Some of items do not contain CommonPart or TitlePart, so I can't use them as parameter to get ContentItem.ID.

My actual code looks like this:

private IContentQuery<ContentItem> GetContentQuery() {            
    IContentQuery<ContentItem> query = _contentManager.Query(VersionOptions.Latest, 
                                                             Manager.DocConfig.Keys.ToArray());

        if (Manager != null) {
            switch (Manager.SortBy) {
                case SortOrder.Id: return query;
                                   // return query.OrderByDescending<CommonPartRecord>(cpr => cpr.Id);
                case SortOrder.Published : 
                                    return query.OrderByDescending<CommonPartRecord>(cpr => cpr.PublishedUtc);
                case SortOrder.TitlePart : 
                                    return query.OrderBy<TitlePartRecord>(tp => tp.Title);
                case SortOrder.Custom :
                                    var sf = Manager.SortFunc;
                                    return (sf == null) ? query : sf(query);
            }
        }

        return query;
    }

is it possible?

1
How about .OrderByDescending<ContentPartRecord>(c => c.Id);? - devqon
@devqon ContentItem does not contain ContentPartRecord directly. other Part classes inherite from it. So using OrderByDescending<ContentPartRecord>() throws an exception. Each ContenItem always contains only InfoSetPart, but it doesn't inherite from ContentPart - teran
You are correct, my bad :) - devqon
Why do you want to order by id? - Bertrand Le Roy
@BertrandLeRoy actually I want to sort them in creation order (they are equal, right?), but anyway I don't have CommonPart in each item to get CreationDate. - teran

1 Answers

-1
votes

The creation date on the common part is the correct way of ordering by creation date. Using the id is a hack that can fail in a number of ways. The common part should really be on all content types.