我希望簡化以下代碼包含foreach循環以最小化的迭代和/或增加,因爲LINQ和集合的性能在每次迭代被創建:foreach循環(與包括LINQ)性能提高
foreach (Contact contact in Contacts) // phone contacts, around 500-1000
{
IEnumerable<ContactEmailAddress> emails = contact.EmailAddresses; // each has multiple emails
foreach (Friend parseUser in parseUsers) // could be many thousands
{
if (emails.Where(e => e.EmailAddress == parseUser.Email).ToList().Count > 0)
{
parseUser.AddContact(contact); // function call
verifiedUsers.Add(parseUser); // add to my new aggregated list
}
}
}
感謝。
'如果(emails.Any(E => e.EmailAddress == parseUser.Email))' – Romoku 2013-04-25 16:20:32
開始'.ToList()計數>由'。任何0'() ' – ken2k 2013-04-25 16:20:39
@Romoku&肯,而那無線肯定會有所幫助,因爲它增加了短路,你可以通過完全避免嵌套循環內部的線性搜索來做得更好。 – Servy 2013-04-25 16:22:27