Pages

Tuesday, September 24, 2013

Copy unique rows of a column from one Datatable to other Datatable - LINQ



Here , We have to copy the unique rows of column 'CategoryName' from DataTable 'dt_old' to the 'Category' column of DataTable 'dt_Categories' using LINQ.


 DataTable dt_Categories = new DataTable();
 dt_Categories.Columns.Add("Category", Type.GetType("System.String"));

DataTable dt_old = new DataTable();
dt_old = (DataTable)Session["dt"];


 dt_Categories = dt_old.AsEnumerable().Select(row =>
            {
                DataRow newRow = dt_Categories.NewRow();
                newRow["Category"] = row.Field<string>("CategoryName");
                return newRow;
            }).Distinct(DataRowComparer.Default).CopyToDataTable();






No comments:

Post a Comment