How to filter out duplicate items using MVVM

Posted on 2011-10-06
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
    LVL 26

    Expert Comment

    by: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
    LVL 27

    Accepted Solution

    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

    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

    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

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

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    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.…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now