Avatar of ichikuma
ichikuma
Flag for United States of America asked on

How to filter out duplicate items using MVVM

I'm trying to use one DataContext (ViewModel) to load my data once, and then use it in three different views.  I have it working for two of my views but the third one is my problem case.  Here's what I'm dealing with.

I have an ObservableCollection of Events, that contain EventDetails which contains a list of participants.
public class Events: ObservableCollection<EventDetails>
{
} 

public class EventDetails : ObservableCollection<Participants>
{
}

Open in new window

The first two views are easy, the first view is just a list of Event->Name.  The second view is also pretty simple, it's the list of Events->Details->Participants in a TreeView.  The third view is much more difficult.  I want to display all of the Names of the Participants from all of the events but only showing 'unique' ones, no duplicates and put them in a ListView.

Is there a way to somehow use databinding while filtering out duplicate data?
Microsoft Development.NET Programming

Avatar of undefined
Last Comment
ichikuma

8/22/2022 - Mon
Anurag Thakur

try to remove the duplicates by using a LINQ query
You will have to filter the duplicates before you do the data binding

sample for how to remove duplicates from LIST<T> using LINQ
http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq
ASKER CERTIFIED SOLUTION
MikeToole

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ichikuma

ASKER
Thanks for the quick response.  @MikeToole, if there are 10 events, would the property then hold all the participants names from all 10 events?
MikeToole

Since the Distict() method is applied to the List(of string) output by the Select() method, it will eliminate any duplicates and return a distinct list of participants no matter how many events there were.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ichikuma

ASKER
Exactly what I needed explained in a manner that made sense to me.