IList question

4 posts, 0 answers
  1. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 01 Sep 2010 Link to this post

    Hi all,

    Just a general question about an IList. I posted this around the web, but actually with Telerik Forums I'm almost certain I'll get an anwer! :)

    I want to remove some duplicates from an IList, but I'm not sure how to do it?
    I've a List<Contact> which contains three fields: a name and an emailaddress and a type.

    So I use it like this:
    contactList.Add(new Contact(item.Name, item.Email, "GMAIL"));

    What I want is to remove or mark records which have the same emailaddress.

    How can I do such thing within a generic list?

    Thanks!
    Daniel
  2. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 07 Sep 2010 Link to this post

    Assume this ...
    public class Contact
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }
    And a List defined as ...
    List<Contact> contacts = new List<Contact>
    {
        new Contact{Name="Stuart Hemming1", Email="sejhemming@gmail.com"},
        new Contact{Name="Stuart Hemming2", Email="sejhemming2@gmail.com"},
        new Contact{Name="Stuart Hemming3", Email="sejhemming3@gmail.com"},
        new Contact{Name="Stuart Hemming4", Email="sejhemming4@gmail.com"},
        new Contact{Name="Stuart Hemming5", Email="sejhemming5@gmail.com"},
        new Contact{Name="Stuart Hemming6", Email="sejhemming@gmail.com"}
    };
    Then you can get a list of contacts with duplicate Email values using ...
    contacts.GroupBy(c => new { Email = c.Email }).Select(g => g.ToList()).ToList().Where(l=>l.Count > 1);

    Am I helping yet?

    -- 
    Stuart
  3. Ivan Dimitrov
    Admin
    Ivan Dimitrov avatar
    78 posts

    Posted 07 Sep 2010 Link to this post

    Hi,

    • To remove some items you will need to create  a new list and populate it, because you could get an error that you cannot modify the collection. Also, List<T> supports removing at an index.
    • You can use directly Distinct.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 07 Sep 2010 Link to this post

    Ivan is right, of course, you will need a new list if you want the duplicates removed. Something like this would work...
    List<Contact> contacts = new List<Contact>
    {
        new Contact{Name="Stuart Hemming1", Email="sejhemming@gmail.com"},
        new Contact{Name="Stuart Hemming2", Email="sejhemming2@gmail.com"},
        new Contact{Name="Stuart Hemming3", Email="sejhemming3@gmail.com"},
        new Contact{Name="Stuart Hemming4", Email="sejhemming4@gmail.com"},
        new Contact{Name="Stuart Hemming5", Email="sejhemming2@gmail.com"},
        new Contact{Name="Stuart Hemming6", Email="sejhemming@gmail.com"}
    };
    var list = contacts.GroupBy(c => new { Email = c.Email }).Select(g => g.ToList()).ToList().Where(l=>l.Count > 1);
    List<Contact> dups = new List<Contact>();
    foreach (List<Contact> i in list)
    {
        dups.AddRange(i);
    }
    List<Contact> clean = contacts.Except(dups).ToList();
    Of course, the 'clean' list doesn't have any of the duplicate entries in, so you might want to post-process the 'dups' list.

    Ivan is also right that would could use Distinct, but then you'd have to go to the effort of writing a Comparer.

    -- 
    Stuart
Back to Top