0

I want to get all common rows (including duplicate rows) between two data table having more than 45000 rows.

However, I want to skip "Column 2" and "all columns after column 6" out of comparison scope.But I am getting no rows as intersection result. Could any one suggest me where I am doing wrong?

Here's my code written:

CustomDataRowComparer myDRComparer = new CustomDataRowComparer();
var commonData = outputTable.AsEnumerable().Intersect(prevTable.AsEnumerable(), myDRComparer).CopyToDataTable();



   public class CustomDataRowComparer : IEqualityComparer<DataRow>
    {
        public bool Equals(DataRow x, DataRow y)
        {
            for (int i = 0; i < 7; i++)
            {


                if (i !=1) // Don't want to consider this column for comparison
                {
                    if (x[i].ToString() != y[i].ToString())
                    {
                        return false;
                    }
                }

            }
            return true;
        }

        public int GetHashCode(DataRow obj)
        {
            return obj.ToString().GetHashCode();
        }
2
  • I think the hash is wrong. Try this : return string.Join("^",obj.ItemArray.Select((x,i) => i == 1 ? "" : x.ToString()).ToArray()).GetHashCode(); Commented Jul 21, 2016 at 11:10
  • I just realized if you use my hash the Equals() method can just compare DataRow x to DataRow y the hash will be unique and will determine where rows match. Commented Jul 21, 2016 at 11:19

1 Answer 1

1

Here is full code

    public class CustomDataRowComparer : IEqualityComparer<DataRow>
    {
        public bool Equals(DataRow x, DataRow y)
        {
            return x.Equals(y);
        }

        public int GetHashCode(DataRow obj)
        {
            return string.Join("^", obj.ItemArray.Select((x, i) => i == 1 ? "" : x.ToString()).ToArray()).GetHashCode();
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

How can I adapt lambda expression to exclude two more columns? Right now, You are excluding i=1. Can we adapt it to exclude i>=7 ?
I adjusted my data table so that I have 6 columns in beginning that I would like to consider for comparison and I changed the return statement as 'return string.Join("^", obj.ItemArray.Select((x, i) =>i>5? "" : x.ToString()).ToArray()).GetHashCode();' But it's not working still.
Are you getting back any rows that compare? Try comparing the same datatable table against itself to see if the code works. All rows should be returned.
If I compare same data table, it works fine and return all rows. But it's not working when I am comparing it to another table.It return zero row. However, another table has equal rows if we ignore column 2 value and columns values after column 6.
Try : (i == 1) || (i >= 5) ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.