Pages

Showing posts with label duplicate rows. Show all posts
Showing posts with label duplicate rows. Show all posts

Thursday, July 11, 2013

Remove the Duplicate Rows from Data Table using LINQ - C#

"dt" is the datatable with the duplicate rows , now we have to remove the duplicate rows 

the field on behalf on which we are removing the duplicate rows is the "Name" field.

var s = dt.AsEnumerable().GroupBy(t => t[0]).Where(t1 => t1.Count() > 1).ToList();


// make new datatable same as the dt, which will contains the unique rows

         DataTable dt_new = dt.Clone();   

// get rows from var "s" and insert into the new datatable "dt_new"

        foreach (var item in s)
        {
            DataRow dr =
dt_new.NewRow();
            dr["Name"] = item.Key.ToString();
           
dt_new.Rows.Add(dr);
        }


"dt_new" is the datatable contains all unique rows acc. to the Name

Remove the Duplicate Rows from Data Table on the Behalf of Particular Column.

In this Example, I am Removing the Duplicate Rows From DataTable "dt" according to the Field "Country"

Lets have a look.........


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView2.aspx.cs"
    Inherits="GridViewSorting2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GrdDetails" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None" AutoGenerateColumns="false" >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lbl_country" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <br />
        <br />
        <asp:Button ID="btn_RemoveDuplicateResults" runat="server" Text="Remove Duplicate Results"
            OnClick="btn_RemoveDuplicateResults_Click" />
    </div>
    </form>
</body>
</html>
=====================================



=========================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class GridView2 : System.Web.UI.Page
{
 
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

        DataColumn col;
        DataRow row1;

        col = new DataColumn();
        col.ColumnName = "Country";
        col.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(col);

        for (int i = 0; i < 2; i++)
        {
            row1 = dt.NewRow();
            row1["Country"] = "Delhi";
            dt.Rows.Add(row1);
        }
        for (int i = 0; i < 1; i++)
        {
            row1 = dt.NewRow();
            row1["Country"] = "Mumbai";
            dt.Rows.Add(row1);
        }
        for (int i = 0; i < 5; i++)
        {
            row1 = dt.NewRow();
            row1["Country"] = "kolkata";
            dt.Rows.Add(row1);
        }
        BindGridview();
    }

    public void BindGridview()
    {
        GrdDetails.DataSource = dt;
        GrdDetails.DataBind();
    }
 
  
    protected void btn_RemoveDuplicateResults_Click(object sender, EventArgs e)
    {
        Hashtable hTable = new Hashtable();
        ArrayList duplicateList = new ArrayList();

        //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
        //And add duplicate item value in arraylist.
        foreach (DataRow drow in dt.Rows)
        {
            if (hTable.Contains(drow["Country"]))
                duplicateList.Add(drow);
            else
                hTable.Add(drow["Country"], string.Empty);
        }

        //Removing a list of duplicate items from datatable.
        foreach (DataRow dRow in duplicateList)
            dt.Rows.Remove(dRow);

        //Datatable which contains unique records will be return as output.
        DataTable dt22 = dt.Copy();

        BindGridview()
    }
}