This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

12 May 2017

Get Comma (delimiter) Separated String from DataTable Column Values in C#.Net

In this Programming Post, we will see an example program to get string of comma separated values from DataTable Column in C#.Net.

Generally while working with DataTable, we may need to generate a comma (or any delimiter) separated string and even we may need to generate comma separated values with single quotes to use the string in any sort of Data Filter or to pass it for sql query IN or NOT IN clause etc,. We will achieve this in the following program.

Program :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Linq;
using System.Data;

namespace DataTableColumnCommaSeperated
{
    class Program
    {
        static void Main(string[] args)
        {
            //Printing Dictionary
            Console.WriteLine("\n*** www.programmingposts.com ***\n");
            Console.WriteLine("\n>>> C# Program to Get Comma Separated String from DataTable Column Values <<<\n");

            DataTable dt = getDepartments();

            //Generating string array from DataTable Column DeptId
            string[] strArr = dt.AsEnumerable().Select(r => Convert.ToString(r["DeptId"])).ToArray();

            //Generating CommaSeperated string from String Array
            string strCommaSeperated = string.Join(",", strArr);

            //Generating CommaSeperated string with single quoted values from String Array
            string strCommaSeperatedWithSingleQuotes = string.Join(",", strArr.Select(r => "'" + r + "'"));

            Console.WriteLine("Printing Comma Seperated String ");
            Console.WriteLine(strCommaSeperated);

            Console.WriteLine("Printing Comma Seperated String with single quoted values");
            Console.WriteLine(strCommaSeperatedWithSingleQuotes);

            Console.ReadLine();
        }

        /// <summary>
        /// returns sample DataTable
        /// </summary>
        /// <returns></returns>
        private static DataTable getDepartments()
        {
            DataTable dtDept = new DataTable();
            dtDept.Columns.Add("DeptId", typeof(Int32));
            dtDept.Columns.Add("DeptName", typeof(String));
            dtDept.Columns.Add("DeptEmpCount", typeof(Int32));
            DataRow dr;

            for (int i = 1; i <= 5; i++)
            {
                dr = dtDept.NewRow();
                dr["DeptId"] = i;
                dr["DeptName"] = "Dept" + i.ToString();
                dr["DeptEmpCount"] = (i * 10);
                dtDept.Rows.Add(dr);
            }
            return dtDept;
        }
    }
}

Output:


output text :
 *** www.programmingposts.com ***

 >>> C# Program to Get Comma Separated String from DataTable Column Values <<<

 Printing Comma Seperated String
 1,2,3,4,5
 Printing Comma Seperated String with single quoted values
 '1','2','3','4','5'