2010-11-24 54 views
3
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)); 

在上面的代码中,我试图获得不同的域名的电子邮件ID,但我得到他们所有什么问题与我的逻辑?谷歌联系人中的不同域名

测试数据:

INP:

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

... 这样20,000项

我需要得到DISTINCT

但我的O/P是

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

实际上它应该是:

gmail.com yahoo.com someOtherDomain.com

+1

使用列表这是O(n^2),使用HashSet而不是O(n)。但是我在这段代码中看不到一个错误。 – CodesInChaos 2010-11-24 14:19:55

+0

使用`string []`作为输入来测试您的代码,并且它正常工作(除了区分大小写)。 – CodesInChaos 2010-11-24 14:25:43

回答

1

这可能并不清楚实际上错在这里,但它是这样做的效率低下和丑陋的方式。我建议你试试这个:

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(); 

话虽如此,你原来的代码真的应该工作。你能否提供一些失败的测试数据?