Click here to Skip to main content
11,414,234 members (71,801 online)
Click here to Skip to main content

Tagged as

String concatenation using LINQ to create a CSV/PSV string

, 26 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A tip to concatenate a set of strings using comma/pipe

Consider you have a set of string names: Chin, Rahul, and John. And you would like to build a comma separated string from the collection. We can do this using the Aggregate extension in LINQ as follows:

IEnumerable<string> sList = 
  new List<string> {"Chin", "Rahul", "John"};
var commaSeparated = sList.Aggregate((x, y) => x + "," + y);
var pipeSeparated = sList.Aggregate((x, y) => x + "|" + y);

Here is the expected output:



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

GeneralI think this will be slower compared to the string.Join meth... Pin
RakeshMeena, 26-Jun-11 20:18
memberRakeshMeena26-Jun-11 20:18 
GeneralWhat is the performance difference between that and Join(sLi... Pin
dgauerke, 26-Jun-11 11:04
memberdgauerke26-Jun-11 11:04 
GeneralWill it not be slow enough to go this way?! Pin
Ravi Sant, 26-Jun-11 9:37
memberRavi Sant26-Jun-11 9:37 
GeneralYou need to think about escaping as well as (pointed out alr... Pin
Henry.Ayoola, 23-Jun-11 22:34
memberHenry.Ayoola23-Jun-11 22:34 
GeneralThink there are syntax errors in the code. list.Aggregate sh... Pin
George Swan, 23-Jun-11 21:16
memberGeorge Swan23-Jun-11 21:16 
GeneralRe: a type mistake. thanks for pointing out. Pin
JP_Rocks, 26-Jun-11 9:35
memberJP_Rocks26-Jun-11 9:35 
GeneralWould work OK for short lists. Would probably go with a Stri... Pin
AspDotNetDev, 23-Jun-11 6:16
protectorAspDotNetDev23-Jun-11 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.2 | Last Updated 26 Jun 2011
Article Copyright 2011 by JP_Rocks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid