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.

+3


source to share


1 answer


If the items are filled in correctly, you should get the results you want using the following code:



var categoryList = categories.Split(',').Select(c=>int.Parse(c));   
Items = Items.Where(x => categoryList.Contains(x.categoryId)).ToList();

      

+2


source







All Articles