OrderBy clause on Product selection

Vote:
 

Hi,

I am trying to get top 24 products in a category based on "PublishedDate" property. So far I got this :

var booksList = _contentLoader.GetDescendents(categoryContentLink)
.Where(p => _contentLoader.Get(p, Helper.GetCultureInfo(CountryCode)) is BookProduct)
.Select(p => _contentLoader.Get(p, Helper.GetCultureInfo(CountryCode)))
.OrderBy(p => p.PublishedDate)
.Take(24);

foreach(var book in booksList)
{
var product = _contentLoader.Get(book.ContentLink, "en-GB");
}

If remove OrderBy clause then everything works but with OrderBy clause system hangs on foreach loop and doesnt excute further.

Please suggest if I am doing anything wrong.

Thanks,

Anurag

#187217
Jan 16, 2018 11:44
Vote:
 

I moved this discussion to Commerce forum.

First of all this can be much faster if you use indexed search, instead of content APIs.

Second of all you are trying to load content multiple times. Try something like this (pseudo code)

var booksList = _contentLoader.GetDescendents(categoryContentLink)
.Select(p => GetBookProduct(p))

.Where(p => p != null)
.OrderBy(p => p.PublishedDate)
.Take(24);

private BookProduct GetBookProduct(ContentReference contentLink)

{

  BookProduct bookProduct;

  _contentLoader.TryGet<BookProduct>(contentLink, Helper.GetCultureInfo(CountryCode), out bookProduct);

 return bookProduct;

}

not sure why the final foreach is needed.

Final of all, are you expecting the category to have subcategories? Otherwise GetChildren should be enough.

#187220
Jan 16, 2018 12:22
Vote:
 

Hi,

Thanks for your reply, I m afraid that didn't speed up things, I guess since I have more than 2K items inside a category it gets stuck in converting them into BookProduct object.

I end up running a loop for each sub-cat and adding Books into a list and then OrderBY and pick top 24, didnt seems to be processor intensive and was very quick. 

To answer you question I have further sub-cat thats why I used GetDescendents().

var booksList = _contentLoader.GetChildren<BookProduct>(categoryContentLink).ToList();

var bookListNode = _contentLoader.GetChildren<ProductListingNode>(categoryContentLink);

foreach (var node in bookListNode)
{
booksList.AddRange(_contentLoader.GetChildren<BookProduct>(categoryContentLink).ToList());

}

 booksList = booksList.OrderBy(p => p.PublishedDate).Take(24).ToList();


Thanks for you help, as always your reply saved the day.

Thanks,

Anurag

#187237
Jan 16, 2018 16:37
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.