I'm working on a blog post system using the Umbraco CMS (v7), where each post can select multiple categories for the users to filter through on the front end; the categories for the blog posts are stored as a comma-separated string, using the category id's as the values.
I plan to then filter the blog posts using query string values, and check if any of the Blog Post categories match any of the id's query string.
Here is some example data:
Query String Categories = 1,5
Blog Post 1 Categories: 1,2,5,7
Blog Post 2 Categories: 6,7
Blog Post 3 Categories: 1,6
Blog Post 4 Categories: 3,4,5
And I would expect the filtering to return Blog Posts 1, 3 & 4 because Blog Post 2's categories don't match any of the id's in the query string.
Here's a bit of my code I've been working on; it doesn't work but you can kind of see what I'm trying to achieve:
var categories = !Request["c"].IsEmpty() ? Request["c"] : ""; // Query String Values
var categoryList = new List<int>(); // New integer list to add the split query string values to
IEnumerable<dynamic> Items = Umbraco.Content(1052).Descendants("BlogPost"); // Dynamic list of blog posts
if(!categories.IsEmpty()) // If the query string has some values
{
foreach(var cat in categories.Split(',')) // Split the query string…
{
categoryList.Add(Convert.ToInt32(cat)); … and convert and add these id's to my integer list
}
// The line that isn't really working, but the line i'm trying to add the filter to
Items = Items.Where(categoryList.Contains(x => x.searchCategories.Any())).ToList();
}
So my question is: how can I check whether any of the items' categories match any of the categories in the query string values to achieve similar results to my example data?
For Umbraco devs: I'm using the multi node tree picker to select the categories from the blog post, the content is restricted to my 'categories' node list.