To concatenate string items, with separators, you can use String.Join
In .NET 3.5 and below, this takes an array as the second parameter, but in 4.0 it has an overload that takes an IEnumerable<T>
, where T
in this case is String
.
Armed with this information, here's the code you want.
For .NET 3.5:
string result = String.Join(", ",
(from p in PersonList
select p.LastName).ToArray());
For .NET 4.0 you can omit the call to ToArray:
string result = String.Join(", ",
from p in PersonList
select p.LastName);
If you want to drop the LINQ-syntax and just use the LINQ extension methods, here's the same in that variant:
For .NET 3.5:
string result = String.Join(", ", PersonList.Select(p => p.LastName).ToArray());
For .NET 4.0 you can omit the call to ToArray:
string result = String.Join(", ", PersonList.Select(p => p.LastName));
Note: The 3.5 variants above of course works in 4.0 as well, they did not remove or replace the old method, they just added one for the typical case.