I have been working on a project and I'm actually refactoring some code. I have encountered myself with lots of foreach and if statements, which could be easily replace with LINQ.
But I have this code snippet, that I wonder how I could make it more functional style.
foreach (var notification in notifications)
{
if (_emailService.SendEmail(notification.Message.Subject, notification.Message.Body, notification.Message.MailTo))
{
successNotificationIDs.AddRange(notification.ID);
}
else
{
errorCount++;
}
}
The SendEmail method of the EmailService returns a bool. If its execution has been successfully, it will add an IEnumerable of Int to a declared collection (successNotificationsIDs). If not, I will increase the errorCount variable.
notificationshave? What type notification ID have? \$\endgroup\$List<T>? Maybe ID is something like integer? \$\endgroup\$SendEmailis executed for a side-effect, it's inherently non functional and you should not use call it from LINQ. \$\endgroup\$