3

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.

mmmoustache
  • 2,273
  • 6
  • 41
  • 62
  • I assume if you use `categories = "1, 5"` then categories.Split contains the values `"1 "` and `"5"` (note the space after 1). Does it work when you replace `foreach(var cat in categories.Split(','))` for `foreach(var cat in categories.Replace(" ","").Split(','))`? Edit: Does the code run properly or do you get any exceptions? – Nils O Apr 30 '15 at 09:32
  • Hi Nils, thanks for pointing that out - there aren't actually any spaces inbetween the values – mmmoustache Apr 30 '15 at 09:34
  • Can't understand where is the problem. Can you detail what "doesn't work" actually the problem is? – Renatas M. Apr 30 '15 at 09:38
  • Hi Reniuz, I've edited my question. Basically I can't figure out how to achieve my example data using the code I've already written (it's pretty close, but I can't get that last line to work) – mmmoustache Apr 30 '15 at 09:45

1 Answers1

3

If Items is filled 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();
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44