Click here to Skip to main content
15,116,516 members
Articles / Programming Languages / C#
Tip/Trick
Posted 28 May 2013

Tagged as

Stats

27.6K views
14 bookmarked

Make Comma Separated List the Easy Way

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 May 2013CPOL
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

C#
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

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

Usage

C#
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

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

David_Wimbley
Software Developer
United States United States
I'm a software developer and enjoy .net, angular, mvc, and many other different languages.

Comments and Discussions

 
QuestionCan the extension method be shortened? Pin
George Swan29-May-13 0:17
MemberGeorge Swan29-May-13 0:17 

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

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