Click here to Skip to main content
14,699,089 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey guys, I'm just wondering the best and quickest solution to my following query. I have a dataTable with duplicate rows but the contents of one of the columns for the duplicate row will be different. I just want to combine the two rows in to one row and merge the column contents.

Example of Datatable

Name | Result
----------------------------------------------------
John | 1,2,3,4,5
Mary | 5,6,7,8
John | 6,7,8,9

End Result

Name | Result
----------------------------------------------------
John | 1,2,3,4,5,6,7,8,9
Mary | 5,6,7,8


Thanks in advance guys
Posted

Hi Try like this, this might help you..
class Program
   {
       static void Main(string[] args)
       {

           DataTable dt = new DataTable();
           dt.Columns.Add("Name", typeof(string));
           dt.Columns.Add("Result", typeof(string));

           dt.Rows.Add("John", "1,2,3,4,5");
           dt.Rows.Add("Mary ", "5,6,7,8");
           dt.Rows.Add("John", "6,7,8,9");

           DataTable dtRsult = dt.Clone();
           var distinctRows = dt.DefaultView.ToTable(true, "Name").Rows.OfType<datarow>().Select(k => k[0] + "").ToArray();
           foreach (string name in distinctRows)
           {
               var rows = dt.Select("Name = '" + name + "'");
               string value = "";
               foreach (DataRow row in rows)
               {
                   value += row["Result"] + ",";
               }
               value = value.Trim(',');

               dtRsult.Rows.Add(name, value);
               value = "";


           }

           var output = dtRsult; // dtRsult has the output, which u needed.



       }

   }
   
v2
Comments
An@nd Rajan10 17-Dec-13 11:27am
   
useful solution..
Karthik_Mahalingam 17-Dec-13 11:45am
   
thanks rajan
frostcox 19-Dec-13 5:57am
   
Cheers dude.
Karthik_Mahalingam 19-Dec-13 7:45am
   
thanks mick :)

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