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:;;

I would like to want it as:

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

foreach (var email in emailAddressesOnRequest)
if (!string.IsNullOrEmpty(email) && email.Contains(';'))

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>
    "; mnop; qrst",

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