Question

 List<string> ls = new List<string>();
 Feed<Contact> f = cr.GetContacts();

        foreach (Contact e in f.Entries)
            foreach (EMail el in e.Emails)
                if (!(ls.Contains(el.Address.Substring(el.Address.LastIndexOf('@')+1))))
                    ls.Add(el.Address.Substring(el.Address.LastIndexOf('@')+1));

In above code, i am trying to get distinct domain of email id, but i am getting them all whats problem with my logic?

test data:

inp:

abca@gmail.com
sdafdf@yahoo.com
sdfs@gmail.com
ssdf@gmail.com
sdfsf@someOtherDomain.com

... such 20,000 entries

i need to get DISTINCT domains

but my o/p is

gmail.com
yahoo.com
gmail.com
gmail.com
someOtherDomain.com

actually it should be:

gmail.com yahoo.com someOtherDomain.com

Was it helpful?

Solution

It's not obvious what's actually wrong here, but it's an inefficient and ugly way of doing it. I suggest you try this:

var domains = (from contact in cr.GetContacts().Entries
               from email in contact.Emails
               let address = email.Address
               select address.Substring(address.LastIndexOf('@') + 1))
              .Distinct(StringComparer.OrdinalIgnoreCase)
              .ToList();

Having said that, your original code really should have worked. Could you provide some test data which is failing?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top