How to filter out duplicate items using MVVM

Posted on 2011-10-06
Medium Priority
Last Modified: 2013-11-12
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?
Question by:ichikuma
  • 2
  • 2
LVL 26

Expert Comment

by:Anurag Thakur
ID: 36928998
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
LVL 27

Accepted Solution

MikeToole earned 2000 total points
ID: 36929747
If all you want is a list of participent names you can set up a new property using Linq to select from your currrent observable collection.
Off the top of my head:

public List<String> ParticipentNames
{ get { return EventDetails.Select(p => p.TheNameField).Distinct().ToList();}}

Remember to invoke property changed on ParticipentNames whenever the Observable Collection changes

Author Comment

ID: 36930505
Thanks for the quick response.  @MikeToole, if there are 10 events, would the property then hold all the participants names from all 10 events?
LVL 27

Expert Comment

ID: 36930809
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.

Author Closing Comment

ID: 36930816
Exactly what I needed explained in a manner that made sense to me.

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question