3 ms·
There is String.Join also used in the article but now there is a overload accepting IEnumerable<String>. String.Join("\n", mylist.Select(foo => foo.Descripti
by danbruc 14y ago
There is String.Join also used in the article but now there is a overload accepting IEnumerable<String>.
String.Join("\n", mylist.Select(foo => foo.Description()).Where(x => x != ""));
Using a method group yields an even more readable code. Okay, it depends - if you are not used to method groups, it looks a bit strange but I like it.
String.Join("\n", mylist.Select(x => x.Description()).Where(String.IsNullOrEmpty));
Note that I slightly changed and inverted the predicate because there is no build-in String.IsNotNullOrEmpty but you could of course write it yourself. I have a small extension method in my tool library that wraps String.Join and takes care of filtering out null references and empty and white space-only strings.
mylist.Select(item => item.Description()).JoinExcludingNullOrWhitespace("\n");