What is the best way to remove duplicates from a datatable?

前端 未结 12 2079
醉酒成梦
醉酒成梦 2021-01-02 13:23

I have checked the whole site and googled on the net but was unable to find a simple solution to this problem.

I have a datatable which has about 20 columns and 10K

12条回答
  •  佛祖请我去吃肉
    2021-01-02 14:15

    I wasn't keen on using the Linq solution above so I wrote this:

    /// 
    /// Takes a datatable and a column index, and returns a datatable without duplicates
    /// 
    /// The datatable containing duplicate records
    /// The column index containing duplicates
    /// A datatable object without duplicated records
    public DataTable duplicateRemoval(DataTable dt, int ComparisonFieldIndex)
    {
        try
        {
            //Build the new datatable that will be returned
            DataTable dtReturn = new DataTable();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dtReturn.Columns.Add(dt.Columns[i].ColumnName, System.Type.GetType("System.String"));
            }
    
            //Loop through each record in the datatable we have been passed
            foreach (DataRow dr in dt.Rows)
            {
                bool Found = false;
                //Loop through each record already present in the datatable being returned
                foreach (DataRow dr2 in dtReturn.Rows)
                {
                    bool Identical = true;
                    //Compare the column specified to see if it matches an existing record
                    if (!(dr2[ComparisonFieldIndex].ToString() == dr[ComparisonFieldIndex].ToString()))
                    {
                        Identical = false;
                    }
                    //If the record found identically matches one we already have, don't add it again
                    if (Identical)
                    {
                        Found = true;
                        break;
                    }
                }
                //If we didn't find a matching record, we'll add this one
                if (!Found)
                {
                    DataRow drAdd = dtReturn.NewRow();
                    for (int i = 0; i < dtReturn.Columns.Count; i++)
                    {
                        drAdd[i] = dr[i];
                    }
    
                    dtReturn.Rows.Add(drAdd);
                }
            }
            return dtReturn;
        }
        catch (Exception)
        {
            //Return the original datatable if something failed above
            return dt;
        }
    }
    

    Additionally, this works on ALL columns rather than a specific column index:

    /// 
    /// Takes a datatable and returns a datatable without duplicates
    /// 
    /// The datatable containing duplicate records
    /// A datatable object without duplicated records
    public DataTable duplicateRemoval(DataTable dt)
    {
        try
        {
            //Build the new datatable that will be returned
            DataTable dtReturn = new DataTable();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dtReturn.Columns.Add(dt.Columns[i].ColumnName, System.Type.GetType("System.String"));
            }
    
            //Loop through each record in the datatable we have been passed
            foreach (DataRow dr in dt.Rows)
            {
                bool Found = false;
                //Loop through each record already present in the datatable being returned
                foreach (DataRow dr2 in dtReturn.Rows)
                {
                    bool Identical = true;
                    //Compare all columns to see if they match the existing record
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (!(dr2[i].ToString() == dr[i].ToString()))
                        {
                            Identical = false;
                        }
                    }
                    //If the record found identically matches one we already have, don't add it again
                    if (Identical)
                    {
                        Found = true;
                        break;
                    }
                }
                //If we didn't find a matching record, we'll add this one
                if (!Found)
                {
                    DataRow drAdd = dtReturn.NewRow();
                    for (int i = 0; i < dtReturn.Columns.Count; i++)
                    {
                        drAdd[i] = dr[i];
                    }
    
                    dtReturn.Rows.Add(drAdd);
                }
            }
            return dtReturn;
        }
        catch (Exception)
        {
            //Return the original datatable if something failed above
            return dt;
        }
    }
    

提交回复
热议问题