2013-03-28 115 views
-1

我有一個字符串被稱爲人的名單。我想結合這些並用逗號分隔它們並將它們存儲在一個名爲totalPeopleNames的變量中。這是我有什麼,但它不工作:如何連接字符串在C#中

string totalPeopleNames = null; 

foreach(var person in people) 
{ 
    Enumerable.Concat(totalPeopleNames, ", " + person.Person.FullName); 
} 
+0

可能重複?](http://stackoverflow.com/questions/4884050/create-comma-separated-strings-c)或http://stackoverflow.co m/questions/330493/join-collection-of-objects-into-comma-separated-string或http://stackoverflow.com/questions/2917571/linq-how-do-i-concatenate-a-list-of-整數 - 到 - 逗號分隔字符串 –

回答

9
var totalPeopleNames = String.Join(", ",people.Select(p=>p.Person.FullName)) 
0

您也可以使用聚合擴展方法:

var result = people.Aggregate((p1, p2) => p1.Person.FullName+ ", " + p2.Person.FullName); 
1

最簡單的方法是使用[創建逗號分隔字符串C#的String.Join

var names = String.Join(", ", people.Select(p => p.Person.FullName));