Check if any identifier in comma separated string matches any identifier in another array of strings
I am working on a blog posting system using Umbraco CMS (v7) where each post can select multiple categories to filter users on the front; categories for blog posts are stored as comma separated string using category id as values.
I plan to then filter the blog posts using query string values โโand check if any of the post post categories match any of the id query string.
Here are some sample 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, and 4, because Blog Post 2 categories don't match any of the IDs in the query string.
Here is some of my code that I am working on; it doesn't work, but you can see what I am 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 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 if any of the item categories match any of the categories in the query string values โโto get similar results in my example data?
For Umbraco Developers: I am using a multiscreen node set to select categories from a blog post, the content is limited to a list of node categories.
source to share