Girish
Girish

Reputation: 81

Filtering For duplicate entries in ICollectionView

I have a class as

Class ABC()
{
    public string Name{get;set;}
    public string Category{get;set;}
}

List formed by this class is having value as :-

 Name = "A", Category = "Alphabet"
 Name = "1", Category = "Numeric"
 Name = "2", Category = "Numeric"
 Name = "B", Category = "Alphabet"
 Name = "A", Category = "Alphabet"

I've applied filter(using ICollectionView) on above list based on category only as "Alphabets" and resulting list is:-

 Name = "A", Category = "Alphabet"
 Name = "B", Category = "Alphabet"
 Name = "A", Category = "Alphabet"

which is working fine but I am not able to filter out this duplicate entry from the list. I am using WPF MVVM. Please help.

Upvotes: 1

Views: 805

Answers (1)

morincer
morincer

Reputation: 944

In the filter callback return true only if the current object has the needed category AND is the first one with its name.

Something like this:

    ...
    ObservableCollection<ABC> Items { get;set}
    ListCollectionView ItemsView { get;set }
    ...
    // View filter logic
    ItemsView.Filter = o =>
            {
                var abc = o as ABC;
                if (abc == null) return false;
                return abc.Category == "Alphabet" &&
                       abc == Items.First(i => i.Name == abc.Name);
            };

Upvotes: 5

Related Questions