Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Patrick Shaun McNicholas 24 posts 206 karma points
    Sep 11, 2021 @ 16:31
    Patrick Shaun McNicholas
    0

    Query nodes based on selected Checkbox List Values

    We're building out a news blog using a checkbox list of pre-defined categories and want to be able to find nodes based on their selected checkbox values. I can't find an up to date method for doing this in Umbraco 8

    Any pointers would be helpful... I'm sure other's are doing this already.

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 11, 2021 @ 17:33
    Sven Geusens
    0

    I presume you want this to automatically pick up new categories as you add them.

    If so examine is probably the easiest. Add a SelectedCategory field to the examine index and on document publish, add all the selected boxes to that field.

    You can then do an examinequery on that field only with all selected values seperated by a space (best to change spaces in category names to _ or something similiar when building the field as the default examine behaviour sees a space as a new term)

    If the categories will not change, since its just a bool. You can do a simple nucache node query

    [untested code]

    var filteredNodes = NewsArea.Children<NewsItem>();
    if(checkbox1 || checkbox2 || checkbox3 || checkbox4){
      // at least one is checked
      filteredNodes = filteredNodes.Where(n => 
        (checkbox1 == false || n.Category1) &&
        (checkbox2 == false || n.Category2) &&
        (checkbox3 == false || n.Category3) &&
        (checkbox4 == false || n.Category4) &&
      )
    }
    
  • Patrick Shaun McNicholas 24 posts 206 karma points
    Sep 13, 2021 @ 16:48
    Patrick Shaun McNicholas
    0

    I'm looking for something more simplified like a tag query.

    I had a similar query setup using Tags where the tag on these pages was setup as Umbraco.TagQuery.GetContentByTag("newsItem") my client wanted to eliminate content editors errors in Tagging, but I don't see an easy way to do the same type of query on a Checkbox List...

    string topicRequest = Request.QueryString["topic"];
    if (topicRequest != null && topicRequest != " "){
        newsItems = Umbraco.TagQuery.GetContentByTag(topicRequest)
            .Where(x => x.IsDocumentType("newsItem"))
            .Where(y => y.IsVisible())
            .OrderByDescending(i => i.CreateDate)
            ;
    }
    

    Is there is a way to do is using some type of .Where(x => x.Value("newsCategories").Contains(queryCategory)

    When I try this

    if(categoryRequest != String.Empty){
        newsItems = newsItems.Where(x => x.Value("newsCategories").Contains(categoryRequest));
    }
    

    I get the following error:

     'object' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains<string>(IQueryable<string>, string)' requires a receiver of type 'IQueryable<string>'
    
  • Amir Khan 1282 posts 2739 karma points
    Sep 13, 2021 @ 19:00
    Amir Khan
    0

    Does it need to be converted to a string? Something like this?

    newsItems = newsItems.Where("Visible").Where("@newsCategories.ToString().Contains(@0)", categoryRequest);
    
  • Patrick Shaun McNicholas 24 posts 206 karma points
    Sep 13, 2021 @ 19:38
    Patrick Shaun McNicholas
    0

    No... while I can convert the element to a string on the node itself during an individual page view. String.Join(", ", Model.NewsCategories.ToList()

    This causes an error when it's in a list of nodes on the parent document.

    newsItems = newsItems.Where(x => String.Join(", ", x.Value<IEnumerable<string>>("newsCategories").ToList()).Contains(categoryRequest));
    

    Compiler Error Message: CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

    There is no direct ToString() function available on these IEnumerable lists as far as I can tell.

  • Amir Khan 1282 posts 2739 karma points
    Sep 13, 2021 @ 19:50
    Amir Khan
    0

    This isn't really an answer, but I feel like its somehow related to this question I posted last week: https://our.umbraco.com/forum/using-umbraco-and-getting-started/107033-generate-list-from-ienumberable

    I had to rush the site out the door so hid that section for now but will post back if I find a solution.

Please Sign in or register to post replies

Write your reply to:

Draft