C# Errors when doing a simple datarow comparer

99封情书 提交于 2019-12-12 04:45:50

问题


I'm basically trying to do a custom datarow comparer, something small and to keep it simple.

Basically i'm trying to compare column Mykey1 in datatableA and Mykey2 in datatableB.

I'm getting three errors, and i'm not sure why they are being thrown or how to correct them. And Yes I know i'm using a for int loop and changing it to a string, but obviously this is just a lab, and yes i will be comparing strings.

Here in the errors.

  1. Cannot implicitly convert type 'int' to 'string'
  2. ConsoleApplication2.MyDataRowComparer' does not implement interface member 'System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)
  3. ConsoleApplication2.MyDataRowComparer' does not implement interface member 'System.Collections.Generic.IEqualityComparer.Equals(System.Data.DataRow, System.Data.DataRow)

Here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable DT1 = dt1();
            DataTable DT2 = dt2();
            IEnumerable<DataRow> idrP = DT1.AsEnumerable();
            IEnumerable<DataRow> idrS = DT2.AsEnumerable();

            MyDataRowComparer MyComparer = new MyDataRowComparer();
            IEnumerable<DataRow> Results = idrS.Except(idrP, MyComparer);
        }



        private static DataTable dt1()
        {
            DataTable DT1 = new DataTable();
            DT1.Columns.Add("Mykey1");
            for (int i = 0; i < 10000; i++)
            {
                DataRow newRow = DT1.NewRow();
                newRow[0] = i.ToString();
                DT1.Rows.Add(newRow);
            }

            return DT1;

        }


        private static DataTable dt2()
        {
            DataTable DT2 = new DataTable();
            DT2.Columns.Add("Mykey2");
            for (int i = 0; i < 20000; i++)
            {
                DataRow newRow = DT2.NewRow();
                newRow[0] = i.ToString();
                DT2.Rows.Add(newRow);
            }

            return DT2;

        }
    }

    public class MyDataRowComparer : IEqualityComparer<DataRow>`
    {
        public string Compare(DataRow x, DataRow y)
        {
            return String.Compare(x.Field<string>("Mykey1"), y.Field<string>("Mykey2"));
        }
    }
}

EDIT

Here is my new DataRowComparer, however it's not returning any results instead i get

{"Index was outside the bounds of the array."}

public class DataRowComparer : IEqualityComparer
    {
        public bool Equals(DataRow x, DataRow y)
        {
            return
             x.ItemArray.Except(new object[] { x["Mykey1"] }) ==
             y.ItemArray.Except(new object[] { y["Mykey2"] });
        }

        public int GetHashCode(DataRow obj)
        {
            var values = obj.ItemArray.Except(new object[] { obj[obj.Table.PrimaryKey[0].ColumnName] });
            int hash = 0;
            foreach (var value in values)
            {
                hash = (hash * 397) ^ value.GetHashCode();
            }
            return hash;
        }
    }

回答1:


String.Compare returns an integer and not a string so you need to change your Compare method signature. That should resolve the first error.

The rest of your errors are due to the fact your are missing needed implementations in your classes, as the error suggest you are missing the System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow) method and the System.Collections.Generic.IEqualityComparer.Equals(System.Data.DataRow, System.Data.DataRow) method.



来源:https://stackoverflow.com/questions/26361407/c-sharp-errors-when-doing-a-simple-datarow-comparer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!