Copied to clipboard

Flag this post as spam?

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


  • Derrik 29 posts 98 karma points
    Nov 04, 2013 @ 18:11
    Derrik
    0

    Listing nodes where checkbox property contains specific value

    I am trying to list all nodes where a checkbox property (Season) contains a specific value. So far I have this, which is working for the most part:

    myNodes.Where("season.Contains(@0)", pfSeason);

    However, the values of Season can be:

    • Early Spring
    • Spring
    • Late Spring
    • Summer
    • Late Summer
    • Fall
    • Winter

    When I attempt to list all nodes that have Spring selected, it returns nodes that have Early Spring and Late Spring selected as well because Spring is contained within those values. How can I pull only nodes that have Spring selected?

     

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 04, 2013 @ 18:21
    Ali Sheikh Taheri
    0

    Hi Derrik

    can you try this?

    myNodes.Where("season.Split(',').Contains(@0)", pfSeason);
    
  • Derrik 29 posts 98 karma points
    Nov 04, 2013 @ 18:28
    Derrik
    0

    Ali, it returned an error:

    No applicable method 'Split' exists in type 'String'

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 04, 2013 @ 18:31
    Ali Sheikh Taheri
    0

    can you tell me what version of Umbraco you are using? are you also using razor?

  • Derrik 29 posts 98 karma points
    Nov 04, 2013 @ 18:34
    Derrik
    0

    Umbraco v6.1.6, and yes I'm using Razor.

    Here is the rest of the code, if that helps:

    var pfSeason = Request["pf-season"];
    var pagesToList = Model.Descendants("Product");
    pagesToList = pagesToList.Where("season.Contains(@0)", pfSeason);
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 04, 2013 @ 18:34
    Jeavon Leopold
    0

    Hi,

    I might be off track here but aren't you just looking for

    myNodes.Where("season == @0", pfSeason);
    

    Jeavon

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 04, 2013 @ 18:38
    Ali Sheikh Taheri
    1

    Jeavon, I think its checkbox so I am assuming that the selection can be multiple so it should generate CSV.

    how about this one!

    var pfSeason = Request["pf-season"];
    var pagesToList = Model.Content.Descendants("Product");
    pagesToList = pagesToList.Where(x => x.GetPropertyValue<string>("season").Split(',').Contains(pfSeason));
    
  • Derrik 29 posts 98 karma points
    Nov 04, 2013 @ 18:38
    Derrik
    0

    Hi Jeavon,

    The property is a checkbox, which can contains multiple values so it may not always equal pfSeason.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 05, 2013 @ 09:25
    Jeavon Leopold
    0

    Ok, I guess you mean checkbox list then. I think Ali's above suggestion should work?

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 12:50
    Ali Sheikh Taheri
    0

    Hi Derrik,

    did the above code worked?

    Cheers

    Ali

  • Derrik 29 posts 98 karma points
    Nov 05, 2013 @ 16:56
    Derrik
    0

    From the trace: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

    Also, maybe worth mentioning this is built with the old Razor macro rather than a Partial View.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 17:04
    Ali Sheikh Taheri
    0

    is your Model dynamic? if so then can you use CurrentPage instead of Model?

  • Derrik 29 posts 98 karma points
    Nov 05, 2013 @ 17:13
    Derrik
    0

    As in CurrentPage.Descendants("Product") ? I tried and got The name 'CurrentPage' does not exist in the current context

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 17:28
    Ali Sheikh Taheri
    100

    ok then here is what you need

    var pfSeason = Request["pf-season"];
    var pagesToList = CurrentModel.Descendants("Product").Items;
    pagesToList = pagesToList.Where(x => x.GetPropertyValue("season").Split(',').Contains(pfSeason)).ToList();
    

    as you might have noticed I've changed the Model to CurrentModel because Model is dynamic whereas CurrentModel is strongly typed.

    I've also added .Items to get the items for the collection.

    I believe the above code should work

    Cheers

    Ali

  • Derrik 29 posts 98 karma points
    Nov 05, 2013 @ 17:33
    Derrik
    0

    Success! Thank you very much!

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 17:37
    Ali Sheikh Taheri
    0

    you are welcome :)

  • Derrik 29 posts 98 karma points
    Nov 05, 2013 @ 18:59
    Derrik
    0

    Sorry, one more quick question regarding this. I'm reusing this logic for a few other properties and I ran into an issue.

    pagesToList = pagesToList.Where(x => x.GetPropertyValue("habit").Split(',').Contains(pfHabit)).ToList();

    On some of the nodes the property (Habit) does not have a value and I get Object reference not set to an instance of an object.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 19:07
    Ali Sheikh Taheri
    3

    here is your answer:

    var pfSeason = Request["pf-season"];
    var pagesToList = CurrentModel.Descendants("Product").Items;
    pagesToList = pagesToList.Where(x => x.HasProperty("season") && x.HasValue("season") && x.GetPropertyValue("season").Split(',').Contains(pfSeason)).ToList();
    

    you need to check if the property has value first. (if all the pages have got property season then you can remove the first criteria (HasProperty). I would leave it, just in case :)

  • Derrik 29 posts 98 karma points
    Nov 05, 2013 @ 19:09
    Derrik
    0

    Ah, that makes sense. Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft