Introduction
Show an easy way to make a comma separated string
from lists/arrays of string
s 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 string
s, 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)
{
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)
{
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