Copied to clipboard

Flag this post as spam?

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


  • K.Garrein 162 posts 626 karma points
    Jun 17, 2013 @ 12:49
    K.Garrein
    0

    Get Distinct() on a List<DynamicNode>

    Hey.

    I create a list with multiple DynamicNode items.
    When I try to get the distinct items in the list, it doesn't remove the double items, I just get the same list...

    Is this a bug or a feature?

    e.g.

    List<DynamicNode> myList = new List<DynamicNode>();

    myList.Add( new DynamicNode( 1 ) );
    myList.Add( new DynamicNode( 2 ) );
    myList.Add( new DynamicNode( 2 ) );
    myList.Add( new DynamicNode( 1 ) );
    myList.Add( new DynamicNode( 3 ) );

    myList = myList.Distinct().ToList();

    Result: the list still contains the 5 items
    myList.Count --> 5

    Thank you.
    K.

  • Lars-Erik Aabech 348 posts 1096 karma points MVP 4x c-trib
    Jun 21, 2013 @ 17:10
    Lars-Erik Aabech
    0

    A DynamicNode is a reference type. Reference types aren't equal unless you've overridden object.Equals() or it's the same instance.

    In your example, the instances at index 0 and 3 are different instances, and hence not equal.

    Distinct can however use a custom equality comparer.

    Have a look here:
    http://msdn.microsoft.com/en-us/library/bb338049.aspx (plain static comparer for a given case)
    and here:
    http://stackoverflow.com/questions/1071609/iequalitycomparer-for-anonymous-type (utility comparer where you specify a lambda for your current need)

  • Ali Sheikh Taheri 470 posts 1647 karma points c-trib
    Jun 21, 2013 @ 17:21
    Ali Sheikh Taheri
    100

    how about this! first check if the item is not in your list then add it like below:

    var mylist = new List();

    if (!mylist.Select(x => x.Id).Contains(1))
        {
            mylist.Add(new DynamicNode(1));
        }
  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Jun 21, 2013 @ 19:55
    Jeroen Breuer
    0

    What version of Umbraco are you using? In the newer version there are some useful extension methods. If you add the Umbraco.Core namespace you'll be able to do this:

    var nodes = myList.DistinctBy(x => x.Id);

    Jeroen

  • K.Garrein 162 posts 626 karma points
    Jun 24, 2013 @ 08:38
    K.Garrein
    0

    I think it must be version 6.0.5 or maybe even newer.

    I think I tried the Distinct( x => x.Id ) but it was not working.

    I changed the code to use a List<int> with the node id's, so I got it working that way.
    When I need something similar again I will retry your other solutions :)

    Thank you.
    Kris.

  • Lars-Erik Aabech 348 posts 1096 karma points MVP 4x c-trib
    Jun 24, 2013 @ 10:22
    Lars-Erik Aabech
    1

    Note that @Jeroen showed a method called DistinctBy, not Distinct. It's a custom extension in umbraco, not the BCL.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies