kenticokentico-13

Kentico 13 IPageRetriever not taking in OrderBy and FirstOrDefault clauses in Query


I'm using the IPageRetriever but am having issues with some of the queryParametersAction

var retriever = await _pageRetriever.RetrieveAsync<News>(
    query =>
    {
        query
        .PathForChildren(path)
        .NestingLevel(depth)
        .Columns(new string[] {
            nameof(News.NodeAliasPath),
            nameof(News.ListableDocumentImage),
            nameof(News.ListableDocumentImageAlt),
            nameof(News.ListableDocumentTitle),
            nameof(News.ListableDocumentRollupText),
            nameof(News.OccurrenceDate),
            nameof(News.BaseContent),
            nameof(News.FeaturedContent),
        })
        .WhereTrue(nameof(News.FeaturedContent)) //works
        //.Where(n => n.FeaturedContent == true) //doesn't work
        .OrderByDescending(n => n.OccurrenceDate) //doesn't work
        .FirstOrDefault(); //doesn't work
 
    },
            cacheSettings => cacheSettings
        .Dependencies((items, csbuilder) => builder.ApplyDependenciesTo(key => csbuilder.Custom(key)))
        .Key($"GetFeaturedItemAsync|{path}|{depth}")
        .Expiration(TimeSpan.FromMinutes(Constants.CACHE_MINUTES))
    );

These work using new DocumentQuery() and PageRetriever uses DocumentQuery and the documentation link to the available params includes all these methods.

Am I getting the order wrong? When I call query.ToString(true) to see the Sql query from it, there is no mention of the above methods in the sql query.

Granted, the documentation does say:

IPageRetriever (Kentico.Content.Web.Mvc namespace) – interface that provides a service for retrieving pages in basic live site scenarios

But have no definition for what basic means.


Solution

  • You are mixing up some Linq statements and Kentico query statements.

    The where clausule can be used with a lot of different conditions, for example you can use .WhereEquals(nameof(News.FeaturedContent), true)

    For the OrderByDescending you should use a string parameter. So that would be .OrderByDescending(nameof(News.OccurrenceDate)).

    For the FirstOrDefault you can use .TopN(1) for the query and then you need to use .FirstOrDefault() after the RetrieveAsync method as the Linq method can't be added to the query that Kentico creates.