Using LINQ, how can I concatenate string properties from itesm in a collection

I have a list of objects in the collection. Each object has a string property called Issue. I want to combine the problem with all elements of the collection and put them on one line. which is the cleanest way to do this with LINQ.

here is the manual way:

string issueList = ""; foreach (var item in collection) { if (!String.IsNullOrEmpty(item.Issue) { issueList = issueList + item.Issue + ", "; } } //Remove the last comma issueList = issueList.Remove(issueList.Length - 2); return issueList; 
+7
source share
2 answers

You can write

 return String.Join(", ", collection.Select(o => o.Issue)); 

In .Net 3.5 you need to add .ToArray() .

+20
source

You can use ToDelimitedString from morelinq .

0
source

All Articles