This is a migrated thread and some comments may be shown as answers.

IList question

3 Answers 47 Views
Miscellaneous
This is a migrated thread and some comments may be shown as answers.
Daniel Plomp
Top achievements
Rank 2
Daniel Plomp asked on 01 Sep 2010, 10:39 AM
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

3 Answers, 1 is accepted

Sort by
0
Stuart Hemming
Top achievements
Rank 2
answered on 07 Sep 2010, 05:59 AM
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
0
Ivan Dimitrov
Telerik team
answered on 07 Sep 2010, 06:21 AM
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.
0
Stuart Hemming
Top achievements
Rank 2
answered on 07 Sep 2010, 06:51 AM
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
Tags
Miscellaneous
Asked by
Daniel Plomp
Top achievements
Rank 2
Answers by
Stuart Hemming
Top achievements
Rank 2
Ivan Dimitrov
Telerik team
Share this question
or