Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:05:48+00:00 2026-06-04T07:05:48+00:00

I have a data table I’ve loaded from a CSV file. I need to

  • 0

I have a data table I’ve loaded from a CSV file. I need to determine which rows are duplicates based on two columns (product_id and owner_org_id) in the datatable. Once I’ve determined that, I can use that information to build my result, which is a datatable containing only the rows that are not unique, and a data table containing only the rows that are unique.

I’ve looked at other examples on here and the code I’ve come up with so far does compile and execute, but it seems to think every row in the data is unique. In reality in the test data there’s 13 rows and only 6 are unique. So clearly I’m doing something wrong.

EDIT: Thought I should note, rows that have duplicates should ALL be removed, not just the duplicates of that row. eg if there are 4 duplicates, all 4 should be removed not 3, leaving one unique row from the 4.

EDIT2: Alternatively, if I can select all duplicate rows (instead of trying to select unique rows) it is fine with me. Either way can get me to my end result.

The code in the processing method:

MyRowComparer myrc = new MyRowComparer();
var uniquerows = dtCSV.AsEnumerable().Distinct(myrc);

along with the following:

public class MyRowComparer : IEqualityComparer<DataRow>
{
    public bool Equals(DataRow x, DataRow y)
    {
        //return ((string.Compare(x.Field<string>("PRODUCT_ID"),   y.Field<string>("PRODUCT_ID"),   true)) ==
        //        (string.Compare(x.Field<string>("OWNER_ORG_ID"), y.Field<string>("OWNER_ORG_ID"), true)));
        return
            x.ItemArray.Except(new object[] { x[x.Table.Columns["PRODUCT_ID"].ColumnName] }) ==
            y.ItemArray.Except(new object[] { y[y.Table.Columns["PRODUCT_ID"].ColumnName] }) &&
            x.ItemArray.Except(new object[] { x[x.Table.Columns["OWNER_ORG_ID"].ColumnName] }) ==
            y.ItemArray.Except(new object[] { y[y.Table.Columns["OWNER_ORG_ID"].ColumnName] });
    }

    public int GetHashCode(DataRow obj)
    {
        int y = int.Parse(obj.Field<string>("PRODUCT_ID"));
        int z = int.Parse(obj.Field<string>("OWNER_ORG_ID"));
        int c = y ^ z;
        return c;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:05:49+00:00Added an answer on June 4, 2026 at 7:05 am

    You could use LINQ-To-DataSet and Enumerable.Except/Intersect:

    var tbl1ID = tbl1.AsEnumerable()
            .Select(r => new
            {
                product_id = r.Field<String>("product_id"),
                owner_org_id = r.Field<String>("owner_org_id"),
            });
    var tbl2ID = tbl2.AsEnumerable()
            .Select(r => new
            {
                product_id = r.Field<String>("product_id"),
                owner_org_id = r.Field<String>("owner_org_id"),
            });
    
    
    var unique = tbl1ID.Except(tbl2ID);
    var both = tbl1ID.Intersect(tbl2ID);
    
    var tblUnique = (from uniqueRow in unique
                    join row in tbl1.AsEnumerable()
                    on uniqueRow equals new
                    {
                        product_id = row.Field<String>("product_id"),
                        owner_org_id = row.Field<String>("owner_org_id")
                    }
                    select row).CopyToDataTable();
    var tblBoth = (from bothRow in both
                  join row in tbl1.AsEnumerable()
                  on bothRow equals new
                  {
                      product_id = row.Field<String>("product_id"),
                      owner_org_id = row.Field<String>("owner_org_id")
                  }
                  select row).CopyToDataTable();
    

    Edit: Obviously i’ve misunderstood your requirement a little bit. So you only have one DataTable and want to get all unique and all duplicate rows, that’s even more straight-forward. You can use Enumerable.GroupBy with an anonymous type containing both fields:

    var groups = tbl1.AsEnumerable()
        .GroupBy(r => new
        {
            product_id = r.Field<String>("product_id"),
            owner_org_id = r.Field<String>("owner_org_id")
        });
    var tblUniques = groups
        .Where(grp => grp.Count() == 1)
        .Select(grp => grp.Single())
        .CopyToDataTable();
    var tblDuplicates = groups
        .Where(grp => grp.Count() > 1)
        .SelectMany(grp => grp)
        .CopyToDataTable();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data table with data rows, which is coming from Ultra grid.
I have a data table which already has some values, plus it is getting
I have a huge data.table in R which contains the result of a experiment:
I have a data table which is updated every day except Saturday and Sunday.
I have a data table in SQL 2008 R2 which is used to store
I have a data table which is populated with data unrelated to drupal content
I have a data.table with two columns: one ID column and one value column.
I have a data table 44 columns wide that I need to write to
I need function, that returns list of strings. I have data in table like
I have two data tables which have a primary key column in common but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.