Copied to clipboard

Flag this post as spam?

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


  • Facius 12 posts 73 karma points
    Nov 18, 2011 @ 18:36
    Facius
    0

    Razer .Where(Id One Of Multiple Ids)

    I'm tryin to do a .Where() Filter on my nodes with multiple Ids.

    I have been trying to use  .ContainAny() with no luck so far.

    Here's what i got:

    var values new Dictionary<string,object>();
    var keywords new List<string>();
           
    keywords.Add("1222");
    keywords.Add("1193");
    values.Add("keywords",keywords);

    // Doesn't Work     
    //Children Children.Where("Id.Contains(1222)")

    // Works
    //Children Children.Where("Convert.ToString(Id).Contains(\"1222\")")

    // Doesn't Work
    //Children Children.Where("Id.Contains(keywords)"values);
    //Children Children.Where("Convert.ToString(Id).Contains(keywords)"values)
    //Children Children.Where("Convert.ToString(Id).ContainsAny(keywords)"values)

    Appreciate your help.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Nov 18, 2011 @ 22:03
    Dan Diplo
    0

    There are a few problems with what you are trying to do:

    Firstly, in general LINQ queries then Contains() operates on an IEnumerable collection not on a single Int. So given that the Id of a node is of type Int, how can an Int contain anything? Saying Id.Contains(1234) makes no sense. How can a single integer number contain anything?

    Your second problem is that Where in Umbraco razor doesn't, as far as I know, implement Contains.

    However, if you have a list of node Ids and want to find nodes in a list of child nodes then you could do something like this:

    List idsList = new List() { 1064, 1065 };
    
    dynamic foundNodes = CurrentModel.Descendants(x => idsList.Contains(x.Id));
    
    foreach (var n in foundNodes)
    {
    @n.Id
    }
    
  • Facius 12 posts 73 karma points
    Nov 18, 2011 @ 22:37
    Facius
    0

    Thanks Diplo.

    Love the cheatsheet, been missing it for awhile :)

    You got me thinking and i'll take a different approach, something like:

    DynamicNodeList Children = Model.NodeById (List< object > Ids)

     

Please Sign in or register to post replies

Write your reply to:

Draft