The below code looks in a string composed of emails seperated by commas and adds an email if it isnt already in the result collection which is also of type string.
string [] oEmails=orderEmails.Split(',');
string[] partEmails= part[Constants.Emails].ToString().Split(',');
foreach(string email in oEmails)
{
if(!partEmails.Contains(email))
{
part[Constants.Emails] += "," + email;
}
}
Is this the best approach to write this logic? I works fine, but I was wondering maybe if there is a way I can consolidate this logic into a lambda expression?
What bothers me is I am doing nothing with these arrays but splitting the input string. They serve no other purpose.
Besides that, you are storing the emails the wrong way. Don’t store them as a comma separated string, store them as a List. That way you don’t have to parse them every time you modify the “collection”.