Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Make Comma Separated List the Easy Way

0.00/5 (No votes)
28 May 2013 1  
How to make a comma separated string using Linq

Introduction

Show an easy way to make a comma separated string from lists/arrays of strings to an enumerable collection of Objects using a property of that object. The usage code shows two ways in which the code could be used. One on an array of strings, the other on a List of object (sample Person class included below) where a comma separated string is made using the Address property from the Person class.

Background

  • Must be running .NET 3.5 or greater

Using the Code

This method is a simple/easy way to create a comma separated list of items from an enumerable collection.

Extension Method

public static class ExtUtils
{
     public static string ToComma<T, 
     TU>(this IEnumerable<T> source, Func<T, TU> func)
     {
          return string.Join(",", 
          source.Select(s => func(s).ToString()).ToArray());
     }
}   

Example Sample Class

public class Person
{
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
}

Usage

static void Main(string[] args)
{
     //Works on Arrays/Lists
     var bunchOstrings = new string[4];
     bunchOstrings[0] = "C#";
     bunchOstrings[1] = "C++";
     bunchOstrings[2] = "Java";
     bunchOstrings[3] = "Sql";

     var commaList1 = bunchOstrings.ToComma(m => m);

     var listPeople = new List<Person>
     {
          new Person {Name = "David", 
          Address = "100 Main Street", 
          City = "Tampa", State = "FL"},
          new Person {Name = "Thomas", 
          Address = "102 Main Street", 
          City = "Orlando", State = "FL"},
          new Person {Name = "Hank", 
          Address = "103 Main Street", 
          City = "Miami", State = "FL"},
          new Person {Name = "Will", 
          Address = "104 Main Street", 
          City = "Key West", State = "FL"}
     };

     var commaList2 = listPeople.ToComma(m => m.Address);
     var commaList3 = listPeople.ToComma(m => m.Name);

     Console.WriteLine("List Of People Address: {0}", commaList2);
     Console.WriteLine("List Of People Names: {0}", commaList3);
     Console.WriteLine("List Of Strings: {0}", commaList1);
} 

Full Code Sample

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ToCommaExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Works on Arrays/Lists
            var bunchOstrings = new string[4];
            bunchOstrings[0] = "C#";
            bunchOstrings[1] = "C++";
            bunchOstrings[2] = "Java";
            bunchOstrings[3] = "Sql";

            var commaList1 = bunchOstrings.ToComma(m => m);

            var listPeople = new List<Person>
            {
                new Person {Name = "David", 
                Address = "100 Main Street", 
                City = "Tampa", State = "FL"},
                new Person {Name = "Thomas", 
                Address = "102 Main Street", 
                City = "Orlando", State = "FL"},
                new Person {Name = "Hank", 
                Address = "103 Main Street", 
                City = "Miami", State = "FL"},
                new Person {Name = "Will", 
                Address = "104 Main Street", 
                City = "Key West", State = "FL"}
            };

            var commaList2 = listPeople.ToComma(m => m.Address);
            var commaList3 = listPeople.ToComma(m => m.Name);

            Console.WriteLine("List Of People Address: {0}", commaList2);
            Console.WriteLine("List Of People Names: {0}", commaList3);
            Console.WriteLine("List Of Strings: {0}", commaList1);
        }
    }

    public static class ExtUtils
    {
        public static string ToComma<T, 
        TU>(this IEnumerable<T> source, Func<T, TU> func)
        {
            return string.Join(",", 
            source.Select(s => func(s).ToString()).ToArray());
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
    }
}

History

  • 05/28/2013 - Published tip/trick

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here