Copied to clipboard

Flag this post as spam?

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


  • simon eriksen 24 posts 113 karma points
    Aug 25, 2017 @ 10:56
    simon eriksen
    0

    OrderBy selfmade property on parentnode razor

    Im trying to order a list of nodes..

    foreach (var item in sizecollection.OrderBy("Parent.wwwidth"))
    {
    
    
        @item.Parent.wwwidth
    
    
    }
    

    this returns an error "Sequence contains no elements"

    this...

     foreach (var item in sizecollection.OrderBy("Parent.Name"))
    {
    
    
        @item.Parent.wwwidth
    
    
    }
    

    Does what you would expect and orders the nodes by their parents names. all nodes in sizecollection has a parent with a nonempty property with the alias "wwwidth".

    Thanks in advance.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Aug 25, 2017 @ 12:17
    Dan Diplo
    0

    I would do it using LINQ like this:

    foreach (var item in sizecollection.OrderBy(i => i.Parent.GetPropertyValue<int>("wwwidth"))
    {
    
    }
    

    Assuming that wwwidth is an int. If it was a string you'd use GetPropertyValue<string> instead.

  • simon eriksen 24 posts 113 karma points
    Aug 25, 2017 @ 12:36
    simon eriksen
    0

    Hello Dan

    Your answer gives me:

    "error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Aug 25, 2017 @ 13:01
    Nik
    0

    Hi Simon,

    How are you declaring sizecollection? Is it a typed object or a dynamic object. I suspect it is dynamic based on the error you are getting.

    Thanks,

    Nik

  • simon eriksen 24 posts 113 karma points
    Aug 28, 2017 @ 08:15
    simon eriksen
    0

    Hi Nik

    Its dynamic.

    var id = Request.QueryString["id"];
    
        var product = Umbraco.Content(id);
    
        var sizelist = product.wwstr.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var sizecollection = Umbraco.Content(sizelist);
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Aug 28, 2017 @ 08:36
    Dan Diplo
    100

    So, if you use a non-dynamic way to initialise sizecollection then my code should work, I think. So try:

    var sizecollection = Umbraco.TypedContent(sizelist);
    
    foreach (var item in sizecollection.OrderBy(i => i.Parent.GetPropertyValue<int>("wwwidth"))
    {
    
    }
    
  • simon eriksen 24 posts 113 karma points
    Aug 28, 2017 @ 09:20
    simon eriksen
    0

    That worked.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft