This project is read-only.

Sorting on custom contentItems

Topics: Customizing Orchard
Jan 23, 2016 at 4:58 PM
Edited Jan 24, 2016 at 11:07 AM
Hey everyone,

I've written my own module to display and filter wines, It was fine in the beginning but now that there are over 300 contentitems in the database its becoming very slow.
I'm reviewing my code and have noticed that putting these 300 in a viewmodel for filtering on them later on is very slow so i want to get rid of the viewmodel and sort directly.

This is what i have now sorting on the viewmodel: (i have for example a price to sort on = "prijs"
return filteredIDs = AlleWijnenContentItems.AsQueryable().Where(finalquery.ToString()).OrderBy(s => s.prijs).Select(c => c.WijnID).ToList();
I know i can get the price from my contentitem like this:
dynamic contentItem = _contentManager.Get(wijnid);
            double prijs = Convert.ToDouble(contentItem.Wijn.Prijs.Value);
But how can I sort directly in the AsQueryable on the contentitem price itself?
I have a IEnumerable list:
IEnumerable<ContentItem> AlleWijnenContentItems
Thank you


Also on the same topic, i was looping over the contenitems to get the terms of the taxonomies, so inside the loop i had this:
List<TermPart> termsvoorcontentitem = _taxonomyService.GetTermsForContentItem(allewijnenIds[tel]).ToList();
Wich x 300 is very costly again, so i replaced it with: (i know the names of the taxonomies and there is only one term allowed)
 var drankenterms = contentItem.Wijn.Dranken.Terms;
                int kleurid = 0;
                foreach (dynamic term in drankenterms)
                    kleurid = term.Id;
Which is better but still too much code and looping,

I would like to get the id of the term directly

i've tried contentItem.Wijn.Dranken.Terms[0].Id and also contentItem.Wijn.Dranken.Terms.Elementat(0).id but that doesn't seem to work, how can i get this directly?

Forgive me my noobnes..

Jan 24, 2016 at 11:05 AM
It looks like you attached a content field called Prijs to your Wijn content type. Before we had the Orchard.Projections module, it would mean you can't query based on that field and you would need to refactor your module and work with a custom content part and record (WijnPart / WijnPartRecord) so you could sort by its Price. But Bertrand wrote an exciting article on how to leverage the tables created and populated by Projections to enable you to query on content fields here:
Jan 24, 2016 at 11:10 AM
Edited Jan 24, 2016 at 11:43 AM

Thanks for your answer, i will try it out, any idead on getting the term id directly (i did the edit the same time you answered :) )


*Edit # 2 :)

Now that i've got your attention :)

I have a function zoekallewijnen() which returns: return allewijnenmetprijslandenkleurlijst (they are viewmodels)

I call this when the page loads and also every time the user applies a filter (which is very bad i know)

How can i cache this in orchard? Concretely on this code, I've looked at examples but don't know how to apply this, i could cache it for a long time because the database almost never changes.