Separate domains in Google Contacts

 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 the above code, I'm trying to get a separate email id domain, but I get them all, what's the problem with my logic?

test data:

in:

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

... such 20,000 entries

I need to get DISTINCTdomains

but my o / p

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

in fact it should be:

gmail.com yahoo.com someOtherDomain.com

+3
source share
1 answer

It is not obvious what is actually wrong here, but it is an inefficient and ugly way to do this. I suggest you try the following:

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 source code really should have worked. Could you provide some test data that is not running?

+1

Source: https://habr.com/ru/post/1776689/


All Articles