Filtering contents by Taxonomy Terms in code?

Topics: Writing modules
Mar 12, 2012 at 6:45 PM

Hi Guys

We have a Deal ContentType which has "Cities" and "Categories" Fields in DealPart.

I need a filtered list of DealParts for using in Controller, so I've created following method in DealService class:

public IEnumerable<DealPart> Get(int skip, int count, string city = "", string category = "")

return _contentManager.Query("Deal").Join<CommonPartRecord>().OrderByDescending(cr => cr.CreaedUtc)
                .WithQueryHintsFor("Deal").Slice(skip, count).ToList().Select(ci => ci.As<DealPart>());


But question is how is it possible to add a filter for Category and City there?

Any helps would be appreciated.



Mar 12, 2012 at 9:57 PM

I don't think you can do this with the DefaultContentManager.Query() method. If you have Orchard 1.4x you can probably accomplish this by using HqlQuery().

The HqlQuery interface lets you create custom joins and where clauses, so you can look up the ids of your Category and City term parts and inner join on those ids in the Term -> ContentItem table (I forgot what it's called off hand).

If you're using Orchard 1.3 you'll have to either write straight SQL queries for content ids or inject the taxonomy repositories and replicate how the Taxonomy service works.

Mar 13, 2012 at 12:24 PM

Thanks for your reply

I've done some research but I couldn't find any documents about using hqlquery in Orchard.

It would be great if someone write a simple post about how is it possible to select contents with hql queries in Orchard 1.4 modules.