Is there any alternate way of avoiding the Foreach loop? [closed]

Don’t bother about O(N). Simple algorithmic complexity is rarely a good answer regarding real-life performance. The expensive part isn’t iterating over the employee list, it’s sending an e-mail.

If allowed by your requirements, you could use a single e-mail with multiple recipients, for example – of course, this will only work if all the e-mails are the same. Otherwise, on some level, you just have to send n separate e-mails.

Of course, there’s still a lot of ways to improve the perceived performance even then. You could queue all the e-mails at once, and show the user they’re waiting in a queue, rather than blocking the UI. You could send each of the e-mails in parallel. You could send the e-mails to a different service (e.g. Microsoft Exchange) to handle the actual sending for you.

In the end, though, if you’re sending 100 different e-mails, you have to send 100 e-mails. No clever algorithm will help you.

Leave a Comment