good-to-know good-to-know - 3 years ago 183
C# Question

Simply traditional foreach to Linq ForEach

I have a list of strings like:

abcd@domain.com
efgh@domain.com
ijkl@domain.com;mnop@domain.com;qrst@domain.com
uvwx@domain.com
yz@domain.com


I would like to want it as:

abcd@domain.com
efgh@domain.com
ijkl@domain.com
mnop@domain.com
qrst@domain.com
uvwx@domain.com
yz@domain.com


So I wrote the code below and it works as expected.

foreach (var email in emailAddressesOnRequest)
{
if (!string.IsNullOrEmpty(email) && email.Contains(';'))
{
emailAddressesOnRequest.AddRange(email.Split(';').ToList());
emailAddressesOnRequest.Remove(email);
}
}


Is there any way to simply it to LINQ ForEach?

Answer Source

What you are looking for is to iterate through the collection and for each item to return an item of a different kind. For that use Select.

Because in your case you possibly want to return from each item a collection of items, and don't want to have them in nested collections use SelectMany on the result of the Split(';') method.

List<string> values = new List<string>
{
    "abcd@domain.com",
    "efgh@domain.com",
    null,
    "ijkl@domain.com; mnop @domain.com; qrst @domain.com",
    "uvwx@domain.com",
    "yz@domain.com"
};

var result = values.Where(value => !string.IsNullOrWhiteSpace(value))
                   .SelectMany(value => value.Split(';')).ToList();

And in query syntax:

var result = (from value in values
             where !string.IsNullOrWhiteSpace(value)
             from email in value.Split(';')
             select email).ToList();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download