Copied to clipboard

Flag this post as spam?

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


  • Thomas 66 posts 88 karma points
    Jan 05, 2012 @ 16:29
    Thomas
    0

    Find and count unique values in a dynamicNodeList

    I have a dynamicNodeList with a set of values.

    First of all I would like to count unique values 

    Secondly I would like to create a new list of with unique values and their number of occurences in the original list.

    Count() doesn't seem to apply for Dynamic Node Lists, and neither does Distinct()

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 05, 2012 @ 16:37
    Tom Fulton
    1

    Hi,

    I managed to count unique values by using .Items, which you can then do a Distinct() on.  Here's an example that finds unique years from a datepicker property:

        var uniqueYears = myList.Items.Select(x => umbraco.library.FormatDateTime(x.GetPropertyValue("myDateProperty"), "yyyy")).Distinct().OrderByDescending(z => z);

    Not sure how to do your second request, but maybe this will help get you started :)

    -Tom

  • Thomas 66 posts 88 karma points
    Jan 10, 2012 @ 13:38
    Thomas
    0

    Thanks - that did the trick for the first problem :o)

  • awm 187 posts 376 karma points
    May 22, 2012 @ 16:13
    awm
    0

    Just what I was looking for, thanks Tom!

  • wolulcmit 357 posts 693 karma points
    Jul 25, 2012 @ 04:23
    wolulcmit
    0

    Trying to stand on the shoulders of Giants here but toppling over:

    var nodes = Model.NodeById(1087);
    var uniqueYears = nodes.Children.Select(x => umbraco.library.FormatDateTime(x.GetPropertyValue("DatePublished"), "yyyy")).Distinct().OrderByDescending(z => z);

    which gives me the following nonsensical error (alas I know not what the flip lambda is or what a delegate or expression tree type is)

    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

    am using Umbraco 4.8 if that helps

  • Douglas Ludlow 210 posts 366 karma points
    Jul 25, 2012 @ 14:38
    Douglas Ludlow
    0

    The Select method doesn't exist on a DynamicNodeList. I don't have any way of testing it at the moment, but try something like the following:

    var nodes = Model.NodeById(1087);
    var uniqueYears = nodes.Children.Items.Select(x => umbraco.library.FormatDateTime(x.GetPropertyValue("DatePublished"), "yyyy")).Distinct().OrderByDescending(z => z);

    If that doesn't work, something like this might:

    var nodes = Model.NodeById(1087);
    var uniqueYears = nodes.Children.Cast<DynamicNode>().Select(x => umbraco.library.FormatDateTime(x.GetPropertyValue("DatePublished"), "yyyy")).Distinct().OrderByDescending(z => z);

     

  • wolulcmit 357 posts 693 karma points
    Jul 26, 2012 @ 14:52
    wolulcmit
    0

    Hi Douglas,

    thanks a lot for your reply...
    of the two examples given both give me the same lambda error as above.

    I ended up finding this thread which did the trick for me (@alex's answer)
    http://our.umbraco.org/forum/developers/razor/19020-Is-there-something-like-%27GroupBy%27

    Would probably prefer to use one of the solutions above but not sure where I'm going wrong. perhaps I'm missing a @using declaration or something silly like that.

    - Tim

Please Sign in or register to post replies

Write your reply to:

Draft