How to get LINQ result into a Comma separated Value

By samjayander thiagarajan

Get the result of your LINQ query into a single comma separated value in C#

Here is the LINQ to convert your LINQ result into a comma separated value:

string returnVal = (from a in b
                                            select a.StringValue).Aggregate(new StringBuilder(),
                                           (c, d) => c.Append(",").Append(@"""" + d + @""""),
                                           (c) => c.ToString().TrimStart(new char[] { ',' })
                                               .TrimEnd(new char[] { ',' }))


Here "b" is the list and StringValue will be the value which want to get converted to comma separated values.

How to get LINQ result into a Comma separated Value  (1308 Views)