Undercover1989 Undercover1989 - 2 months ago 7
C# Question

C# QueryExpression inner enumerator

I have an array of strings with e-mails, comma separated. Example:

string[] array = new [] {
"test@test.com;test2@test.com",
"stack@test.com;test@test.com"
}


I would like an query expression which would allow me to enumerate throug all three e-mail adresses (test@test.com is an duplicate): "test@test.com, test2@test.com, stack@test.com"

My base attempt is following

var enum = array.Select((item) => item.Split(';'));


The result is (of course) an enumerator with 2 items each containing an array with 2 items.

I don't know how to name that what I want and so I wasn't able to find an answer via google. If you could give me a hint or something I would be very grateful.

Answer

You're most likely looking for LINQ SelectMany method :

var enum = array
           .SelectMany(item => item.Split(';'))
           .Distinct()
           .ToArray(); 

This will return a string array with unique emails.