Copied to clipboard

Flag this post as spam?

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


  • Adriano Fabri 459 posts 1602 karma points
    Jul 26, 2018 @ 16:18
    Adriano Fabri
    0

    [SOLVED] Best way to filter nodes by checking a checkBoxList

    Hi, I have a checkboxlist datatype that have three values.

    Now I need to filter nodes by checking inside this checkboxlist that must contains a specific value.

    Now I get the full list of nodes with this code

    var listNodes = CurrentPage.Descendants("article").Where("Visible").OrderBy("date descending");
    

    Each article have a checkBoxList (alias sectionList) that I must use to filter the listNodes.

    I think I can do something like this...

    var listNodes = CurrentPage.Descendants("article").Where("a => a.sectionList.Split(',').Contains('mySection')").Where("Visible").OrderBy("date descending");
    

    I tried many combinations but none worked.

    In most cases I received the error that I can not use the lambda.

    Any idea?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jul 26, 2018 @ 18:56
    Dan Diplo
    0

    I would use a strongly-typed query rather than dynamics. Something like:

    var listNodes = Model.Content.Descendants("article").
    Where(a => sectionList.Split(',').Contains("mySection")).
    Where(n => n.IsVisible()).OrderByDescending(n => n.CreateDate);
    
  • Adriano Fabri 459 posts 1602 karma points
    Jul 26, 2018 @ 22:50
    Adriano Fabri
    0

    Thank you Dan, tomorrow I'll test your solution.

    Have a good day Adriano

  • Adriano Fabri 459 posts 1602 karma points
    Jul 27, 2018 @ 08:37
    Adriano Fabri
    100

    Hi Dan,

    the query you gave me returned some errors, anyway starting from this I made some changes and the final code is:

    var listNodes = Model.Content.Descendants("article")
        .Where(v => string.Join(",", v.GetPropertyValue<IEnumerable<string>>("sectionList")).Split(',').Contains("mySection"))
        .Where(n => n.IsVisible())
        .OrderByDescending(d => d.GetPropertyValue<string>("date"));
    

    Thank you for the support

    Adriano

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jul 27, 2018 @ 09:45
    Dan Diplo
    0

    No problem! I didn't realise that the sectionList was a property of the node which is probably why my version didn't work.

    I would definitely recommend using strongly-typed queries like this rather than using dynamics as these execute faster, you get better intellisense and they're going to be dropped in Umbraco 8.

Please Sign in or register to post replies

Write your reply to:

Draft