Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey All,
I have a datatable.
In that i have two columns namely msgid and empid.
column is like

srno. msgid empname
1 1 akshay
2 1 sameer
3 1 akash
4 2 sidd


now what i want is ..when for single msgid, i want all empnames to be comma separated.


output: akshay,sameer,akash


How can i get this??

thanks
Posted

Hello Shan,

The answer to your problem can be found right here[^] on CodeProject.

Regards,
 
Share this answer
 
Hi,
Try below Extension method.

C#
public static class Extensions
{
/// <summary>
/// Get the Column values as comma separated.
/// </summary>
/// <param name="value">Your Data Table</param>
/// <param name="columnfilter">Name of Column on the basis you want to filter</param>
/// <param name="filterParam">filter value</param>
/// <param name="columnName">Column's values that needs to be comma separated</param>
/// <returns></returns>
public static string GetCommaSeparatedValue(this DataTable value, String columnfilter, String filterParam, String columnName)
{
            string retVal = String.Empty;
            if (value.Rows.Count > 0)
            {
                var rowColl  = value.AsEnumerable();
                retVal   = String.Join(",",(from r in rowColl where r.Field<string>(columnfilter) == filterParam  select r.Field<string>(columnName)));
            }
            return retVal.ToString();
        }
}
</string></string>


Now use this to get comma separated values.

C#
//Sample data.
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Movies", typeof(String)));
dt.Columns.Add(new DataColumn("TvSeries", typeof(String)));

dt.Rows.Add("1", "Prison Break");
dt.Rows.Add("1", "How i Met your mother");
dt.Rows.Add("3", "Lost");
dt.Rows.Add("2", "Prison Break");

//now get the  values from DataTable.
var str = dt.GetCommaSeparatedValue("Movies", "1", "TvSeries");


you can use this anywhere windows or asp.net.
As this sample code you can refine according to your need.

Hope this helps!
 
Share this answer
 
Hi,


C#
public class Row
{
    public int msgid { get; set; }
    public string empname { get; set; }
}

static void Main(string[] args)
{
    List<Row> myDatabase = new List<Row>()
    {
        new Row(){  msgid = 1, empname="akshay"},
        new Row(){  msgid = 1, empname="sameer"},
        new Row(){  msgid = 1, empname="akash"},
        new Row(){  msgid = 4, empname="sidd"},
    };

    string comma =", ";

    int id = 1;
    

    string output = string.Join(comma, (from Row item in myDatabase
                                        where item.msgid == id
                                        select item.empname));
}
 
Share this answer
 
v2
-- Suppress data loading messages
SET NOCOUNT ON
if OBJECT_ID('tempdb..#TeamInfo')>0 DROP TABLE #TeamInfo
-- Create Sample Data
CREATE TABLE #TeamInfo
( srno int IDENTITY,
msgid int,
empname varchar(50)
)

-- Load Sample Data
INSERT INTO #TeamInfo VALUES ( 1, 'Jim' )
INSERT INTO #TeamInfo VALUES ( 1, 'Mary' )
INSERT INTO #TeamInfo VALUES ( 1, 'Bob' )
INSERT INTO #TeamInfo VALUES ( 2, 'Sue' )
INSERT INTO #TeamInfo VALUES ( 2, 'Ralph' )
INSERT INTO #TeamInfo VALUES ( 2, 'Ellen' )
INSERT INTO #TeamInfo VALUES ( 3, 'Bill' )
INSERT INTO #TeamInfo VALUES ( 3, 'Linda' )

--Retrieve desired data
SELECT
t1.msgid,
MemberList = substring(
---begin string
(
SELECT ( ', ' + empname )
FROM #TeamInfo t2
WHERE t1.msgid = t2.msgid
ORDER BY
msgid,
empname FOR XML PATH( '' )
),
3, -- start position
1000 -- end position
)
FROM #TeamInfo t1
GROUP BY msgid

---- Results
--msgid MemberList
--1 Bob, Jim, Mary
--2 Ellen, Ralph, Sue
--3 Bill, Linda
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900