Google 通讯录中的不同域

发布于 2024-10-04 08:27:15 字数 1386 浏览 8 评论 0原文

 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 雅虎网站 一些其他域名.com

 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:

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

...
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-10-11 08:27:15

这里到底出了什么问题并不明显,但这是一种低效且丑陋的做法。我建议你试试这个:

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

话虽如此,你原来的代码确实应该可以工作。您能提供一些失败的测试数据吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文