Removal of Duplicate Rows from Data table Based on Multiple columns

Pradeep

I have data table which contains many duplicate rows i need to filter those rows from data table based upon multiple columns to get distinct rows in resultant data table....

Barcode Itemid PacktypeId

1      100      1

1      100      2

1      100      3

1      100      1

1      100      3

need only rows which contains packtypeid 1,2,3 remaining 4th and 5th row should be removed

I have tried using two methods but none didn't turns for better result

Data table contains more than 10 columns but unique column's is "Barcode", "ItemID", "PackTypeID"

Method-1:

 dt_Barcode = dt_Barcode.DefaultView.ToTable(true, "Barcode", "ItemID", "PackTypeID");

The above method filter's the rows but it returns columns only 3 column values i need entire 10 column values.

Method-2:
                   List<string> keyColumns = new List<string>();
                   keyColumns.Add("Barcode");
                   keyColumns.Add("ItemID");
                   keyColumns.Add("PackTypeID");   
           RemoveDuplicates(DataTable table, List<string> keyColumns)
            {
            var uniqueness = new HashSet<string>();
            StringBuilder sb = new StringBuilder();
            int rowIndex = 0;
            DataRow row;
            DataRowCollection rows = table.Rows;             
            int i = rows.Count;
            while (rowIndex < i)
            {
                row = rows[rowIndex];
                sb.Length = 0;
                foreach (string colname in keyColumns)
                {
                    sb.Append(row[colname]);
                    sb.Append("|");
                }

                if (uniqueness.Contains(sb.ToString()))
                {
                    rows.Remove(row);
                }
                else
                {
                    uniqueness.Add(sb.ToString());
                    rowIndex++;
                }
               }

The Above Method returns exception like there is no rows at position 5

Pradeep

Method 3:

Instead of Trying above 2 methods i found this Linq Method something very useful

     dt_Barcode = dt_Barcode.AsEnumerable().GroupBy(r => new { ItemID = r.Field<Int64>("ItemID"), PacktypeId = r.Field<Int32>("PackTypeID") }).Select(g => g.First()).CopyToDataTable(); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing duplicate rows (based on values from multiple columns) from SQL table

From Dev

updating "multiple" columns of "selected" rows of a data table with duplicate key values

From Dev

Select duplicate rows in multiple columns of a table

From Dev

Update multiple columns with data retrieved from multiple rows of same table

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Dev

copy specific columns from rows with duplicate data

From Dev

Merge Multiple Duplicate rows based on multiple columns in Pandas.Dataframe

From Dev

Python Pandas: Delete duplicate rows based on one column and concatenate information from multiple columns

From Dev

Jquery: How to remove duplicate HTML TABLE rows based on columns values

From Dev

In SQL, how can I delete duplicate rows based on multiple columns?

From Dev

Removing all rows of a duplicate based on value of multiple columns

From Dev

How to select duplicate columns data from table

From Dev

removal of suffixes from multiple files in windows - not duplicate

From Dev

TSQL Duplicate rows based on columns

From Dev

SQL: Duplicate rows based on columns

From Dev

Unix Delete Duplicate rows from csv based on 2 columns

From Dev

Delete rows in data frame based on multiple columns from another data frame in R

From Dev

SQL Select rows with duplicate data across multiple columns

From Dev

Select multiple rows from table based on ID

From Dev

Find duplicate rows in multiple columns

From Dev

Excel - copy Data from multiple columns and rows

From Dev

Remove rows from data.table in R based on values of several columns

From Dev

How do I drop rows from a Pandas dataframe based on data in multiple columns?

From Dev

How to Auto-Number Duplicate Rows Using Sequence Based on Multiple Duplicate Columns (T-SQL)

From Dev

How to Auto-Number Duplicate Rows Using Sequence Based on Multiple Duplicate Columns (T-SQL)

From Dev

Remove rows from a data frame that contain duplicate information across the columns

From Dev

Delete Rows based on another table with key having multiple columns

From Dev

how to filter multiple rows in a sql table based on two columns

From Dev

Ordering rows in table based on multiple columns grouped together

Related Related

  1. 1

    Removing duplicate rows (based on values from multiple columns) from SQL table

  2. 2

    updating "multiple" columns of "selected" rows of a data table with duplicate key values

  3. 3

    Select duplicate rows in multiple columns of a table

  4. 4

    Update multiple columns with data retrieved from multiple rows of same table

  5. 5

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  6. 6

    copy specific columns from rows with duplicate data

  7. 7

    Merge Multiple Duplicate rows based on multiple columns in Pandas.Dataframe

  8. 8

    Python Pandas: Delete duplicate rows based on one column and concatenate information from multiple columns

  9. 9

    Jquery: How to remove duplicate HTML TABLE rows based on columns values

  10. 10

    In SQL, how can I delete duplicate rows based on multiple columns?

  11. 11

    Removing all rows of a duplicate based on value of multiple columns

  12. 12

    How to select duplicate columns data from table

  13. 13

    removal of suffixes from multiple files in windows - not duplicate

  14. 14

    TSQL Duplicate rows based on columns

  15. 15

    SQL: Duplicate rows based on columns

  16. 16

    Unix Delete Duplicate rows from csv based on 2 columns

  17. 17

    Delete rows in data frame based on multiple columns from another data frame in R

  18. 18

    SQL Select rows with duplicate data across multiple columns

  19. 19

    Select multiple rows from table based on ID

  20. 20

    Find duplicate rows in multiple columns

  21. 21

    Excel - copy Data from multiple columns and rows

  22. 22

    Remove rows from data.table in R based on values of several columns

  23. 23

    How do I drop rows from a Pandas dataframe based on data in multiple columns?

  24. 24

    How to Auto-Number Duplicate Rows Using Sequence Based on Multiple Duplicate Columns (T-SQL)

  25. 25

    How to Auto-Number Duplicate Rows Using Sequence Based on Multiple Duplicate Columns (T-SQL)

  26. 26

    Remove rows from a data frame that contain duplicate information across the columns

  27. 27

    Delete Rows based on another table with key having multiple columns

  28. 28

    how to filter multiple rows in a sql table based on two columns

  29. 29

    Ordering rows in table based on multiple columns grouped together

HotTag

Archive