Click here to Skip to main content
16,015,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I have a datatable like bellow.

NoReg |Emresa  |Caracteristicas	        |Origen	     |Destino
3	  |INFOTEP |MINIBUS  CONFORTABLE    |LA VEGA	 |PUERTO PLATA
3	  |INFOTEP |MUSICA  TELEVISOR  WIFI |MAO	     |SANTO DOMINGO OESTE
3	  |INFOTEP |MUSICA  TELEVISOR  WIFI |MONTECRISTI |SANTIAGO DE LOS CABALLEROS
3	  |INFOTEP |MUSICA  TELEVISOR  WIFI |LA VEGA	 |SANTO DOMINGO NORTE


But i want it like this.

NoReg |Emresa  |Caracteristicas	        |Origen	     |Destino
3	  |INFOTEP |MINIBUS  CONFORTABLE    |LA VEGA	 |PUERTO PLATA
3	  |INFOTEP | 			            |MAO	     |SANTO DOMINGO OESTE
3	  |INFOTEP |MUSICA  TELEVISOR  WIFI |MONTECRISTI |SANTIAGO DE LOS CABALLEROS
3	  |INFOTEP |			            |LA VEGA	 |SANTO DOMINGO NORTE

I want to remove duplicate values in Caracteristicas column and want to merge it.
I am new to C# and don't know how can i achieve this.
Any help would be highly appreciated.
Thanks in advance.

What I have tried:

DataSet ds = ReceiptDataDAL.GetData(val);
                DataTable dtResult = ds.Tables[0].Copy();
                DataTable dt = new DataTable();
                DataColumn dc = new DataColumn("col1", typeof(String));
                dt.Columns.Add(dc);
                // var dt1 = ds.Tables[0].AsEnumerable()
                //.Select(g => g.Field<string>("Caracteristicas")).Distinct().OrderBy(k => k).ToArray();
                var distinctRows = ds.Tables[0].AsEnumerable()
               .Select(g => g.Field<string>("Caracteristicas")).Distinct().OrderBy(k => k).ToArray();
                DataRow dr = dt.NewRow();
                
                foreach (string name in distinctRows)
                {
                    int i = 0;
                    var rows = ds.Tables[0].Select("Caracteristicas = '" + name + "'");
                    string value = "";
                    foreach (DataRow row in rows)
                    {
                        value += row["Caracteristicas"] + "\n";
                    }
                    value = value.Trim(',');
                    dr[0] = value;
                    dt.Rows.Add(value);
                    dt.Rows.InsertAt(dr,0);
                    value = "";
                    i++;
                }

                var output = dtResult;
Posted
Updated 5-Jan-20 10:16am

1 solution

If you're getting the data from a database, use DISTINCT. Otherwise, just create your data source in a list, by using LINQ and Distinct.

Enumerable.Distinct Method (System.Linq) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 14089908 6-Jan-20 0:44am    
@Chritian thank you for your comment. As i already said, i am new to C# so please share the sample code if you have.
Thank you!

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