I want to separate each item in a list, but also within each item, split the item if it contains :
eg.
string[] names = {"Peter:John:Connor","Paul","Mary:Blythe"};
name.Dump();
Will show:
Peter:John:Connor
Paul
Mary:Blythe
However, is there any LINQ that I can use, which will provide the following list:
Peter
John
Connor
Paul
Mary
Blythe
I can do this using:
foreach (var person in names)
{
x = person.split(":").ToList();
foreach (var personinlist in x)
{
// personinlist
}
}
...but that seems very long winded, when I'm certain LINQ could be more elegant.
Use SelectMany to flatten results of splitting each name by :
names.SelectMany(n => n.Split(':'))
.Dump();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With