Make CSV from list of string in LINQ
Posted
by CmdrTallen
on Stack Overflow
See other posts from Stack Overflow
or by CmdrTallen
Published on 2010-05-03T14:45:56Z
Indexed on
2010/05/03
14:48 UTC
Read the original article
Hit count: 399
Hi I would like to take a list collection and generate a single csv line. So take this;
List<string> MakeStrings()
{
List<string> results = new List<string>();
results.add("Bob");
results.add("Nancy");
results.add("Joe");
results.add("Jack");
}
string ContactStringsTogether(List<string> parts)
{
StringBuilder sb = new StringBuilder();
foreach (string part in parts)
{
if (sb.Length > 0)
sb.Append(", ");
sb.Append(part);
}
return sb.ToString();
}
This returns "Bob,Nancy,Joe,Jack"
Looking for help on the LINQ to do this in a single statement. Thanks!
© Stack Overflow or respective owner