Filtering by multiple tags

Posted by Community Admin on 04-Aug-2018 16:55

Filtering by multiple tags

All Replies

Posted by Community Admin on 04-Jan-2017 00:00

I created a custom module with an MVC widget that I'm trying to filter the content by tags.  How do I filter by multiple tags?

This works for a single tag:

var myCollection = dmm.GetDataItems(iconType).Where(p => (p.Status == ContentLifecycleStatus.Live && p.Visible == true)).Where("Tags.Contains((" + SelectedTags[0] + "))")

 

I tried a comma delimited string of SelectedTags but that did not work.  

 

Thanks

Posted by Community Admin on 05-Jan-2017 00:00

Hi Tom, based on this documentation http://docs.sitefinity.com/filter-expressions-for-content-items

You should use something like that: 

((Category.Contains("Taxonomy ID") OR Category.Contains("Taxonomy ID"))

Posted by Community Admin on 05-Jan-2017 00:00

Thanks Victor, that isn't the exact syntax, but after much trial and error, I did find a magic combination using dynamic linq.

var Tags = "Visible = true AND Status = Live AND (";

     foreach (Guid Tag in selectedTags)      

               

             Tags += "Tags.Contains((" + Tag + ")) OR ";      

                         

var myCollection = dmm.GetDataItems(iconType).Where(Tags.Substring(0, Tags.Length-4) + ")")

Couple notes...2 sets of parentheses are required for each Tags.Contains.  Tags cannot be a string value, it must be a var with guid value appended to it.  

 

edit: changing my answer after finding a solution.

This thread is closed