Copied to clipboard

Flag this post as spam?

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


  • Kenny Burns 173 posts 305 karma points
    Dec 13, 2011 @ 19:26
    Kenny Burns
    1

    Razor Where Contains

    Hi All,

    This has been driving me daft and i thought it would be quite easy. I am trying to do a select and a number of WHERE's...something like this:

    foreach(var page in currentNode.Children.Random(1).Where("Visible").Where("featuredRecipe").Where(MULTINODETREEPICKER CONTAINS VALUE))

    As you can see (dont even know if this is possible!) but i am trying to filter this so it only returns matches where the multi-node tree picker values contain "x" (querystring)

    Any info... muchly appreciated!

    Thanks

    Kenny

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 13, 2011 @ 19:29
    Sebastiaan Janssen
    1

    I am not sure if this works, but have you tried:

    foreach(var page in currentNode.Children.Random(1).Where("Visible").Where("featuredRecipe").Where("myPickerProperty.Contains(\"bla\")"))

    Of course you can always fall back on doing it inside the foreach: 

     

    foreach(var page in currentNode.Children.Random(1).Where("Visible").Where("featuredRecipe")) {
    if (page.myPickerProperty.Contains("bla") {
    //do something
    }

     

     

  • Kenny Burns 173 posts 305 karma points
    Dec 13, 2011 @ 20:10
    Kenny Burns
    0

    Hi Sebasiaan,

    Thanks for the reply - the first way looks correct and if i use this method using say bodyText this seems to work fine.

    e.g. foreach(var page in currentNode.Children.Random(1).Where("Visible").Where("featuredRecipe").Where("bodyText.Contains(\"bla\")"))

    However, it does not seem to want to play when I am referencing a multi node tree picker...

    I would probably need it to work the first way. If not i can always go back and try it in XSLT!

    Thanks -

    Kenny

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 13, 2011 @ 20:12
    Sebastiaan Janssen
    0

    Oh  no, not XSLT! ;-)

    Okay, try to do Where("myPickerProperty.ToString().Contains(\"bla\") instead. There is some upcasting going on, so the picker likely doesn't get represented as a string.

  • Kenny Burns 173 posts 305 karma points
    Dec 13, 2011 @ 20:20
    Kenny Burns
    0

    :(

    I know, i am quite enjoying Razor but this really is driving me insane!

    Still no luck - would it be something like myPickerProperty.Values.ToString().Contains(\"bla\")

    ???

  • Kenny Burns 173 posts 305 karma points
    Dec 13, 2011 @ 20:25
    Kenny Burns
    2

    Got it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! it was InnerText that was required!

    foreach(var page in currentNode.Children.Where("Visible").Where("pageFilters.InnerText.ToString().Contains(\"1248\")").Random(1))

    Thanks a lot Seb - this is the reason i love this community! Together we can achieve :)

    Kenny

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 13, 2011 @ 20:31
    Sebastiaan Janssen
    0

    Awesome! To be honest, it shouldn't be this hard, but I'm glad you got it to work.

  • Kenny Burns 173 posts 305 karma points
    Dec 13, 2011 @ 20:41
    Kenny Burns
    0

    Thanks again :)

  • Amir Khan 1287 posts 2744 karma points
    Apr 28, 2015 @ 18:24
    Amir Khan
    0

    Hi,

    Related to this, why would the following return nodes containing "2742", but the latter not return anything?

    //works

    foreach (var post in selection.Where("postCategories.ToString().Contains(\"2742\")").OrderBy("BlogPostDate desc").Take(20))

    // doesn't

    string categoryId = "2742";
    foreach (var post in selection.Where("postCategories.ToString().Contains(\"@categoryId\")").OrderBy("BlogPostDate desc").Take(20))

     

    Thanks for your help,
    Amir

  • Daniel 1 post 71 karma points
    Aug 22, 2016 @ 15:12
    Daniel
    0

    Hi,

    This is a pretty old question but maybe is useful for someone else.

    @Amir - The solution to your problem is as follow:

    string categoryId = "2742";
    foreach (var post in selection.Where("postCategories.ToString().Contains(\"" + @categoryId + "\")").OrderBy("BlogPostDate desc").Take(20))
    

    The key point is to add + before and after your variable.

    Cheers,

Please Sign in or register to post replies

Write your reply to:

Draft