C# - how to get distinct items from a Collection View

▼魔方 西西 提交于 2021-02-17 04:30:10

问题


CollectionView view = CollectionView)CollectionViewSource.GetDefaultView(MyData);
View.Filter = i => ((MyClass)i).MyProperty;

I have a collection view like in the above. The problem is that the collection MyData which has already been bound to a listview contains duplicate items. How do I get unique items as the result of my filtering?


回答1:


This method works:

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(c.Items);
view.Filter = i =>
{
    var index1 = c.MyData.FindIndex(delegate (MyClass s)
    {
        return Object.ReferenceEquals(s, i);
    });
    var index2 = c.MyData.FindLastIndex(delegate (MyClass s)
    {
        return ((MyClass)i).MyProperty == s.MyProperty as string; //value comparison
    });
    return index1 == index2;
};

index1 is the index of the object in the collection. You need to compare the references to get that.

index2 is the last index of the value. There you need to compare the value.

So you basically compare if the index of the current element is the last one where it occurs.

Note:

this does not work naturally with simple types. I had to initialize my test collection like this:

new List<string>() { new string("I1".ToCharArray()), new string("I1".ToCharArray()), "I2" };




回答2:


You can try this

var g = collection.Select(i => i.Property1).Distinct();

And please give a feedback, if it work.




回答3:


Looks like I'm late while figuring out the best solution that fits your need. Anyway I'm providing it because it's cleaner and faster than the accepted.

First as usual let define a function that encapsulates the logic

static bool IsDuplicate(IEnumerable<MyObject> collection, MyObject target)
{
    foreach (var item in collection)
    {
        // NOTE: Check only the items BEFORE the one in question
        if (ReferenceEquals(item, target)) break;
        // Your criteria goes here
        if (item.MyProperty == target.MyProperty) return true;
    }
    return false;
}

and then use it

var view = (CollectionView)CollectionViewSource.GetDefaultView(MyData);
view.Filter = item => !IsDuplicate((IEnumerable<MyClass>)view.SourceCollection, (MyClass)item);



回答4:


In addition to the answer from @Sebastian Tam

var g = collection.Select(i => i.Property1).Distinct();

If collection is a sequence from a User-defined class of your own, you need to implement IEquatable Interface for Distinct to use the default Comparer.

Have a look at this post

What is the default equality comparer for a set type?



来源:https://stackoverflow.com/questions/32756641/c-sharp-how-to-get-distinct-items-from-a-collection-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!