执行简单数据行比较器时的C#错误

执行简单数据行比较器时的C#错误,c#,datatable,datarow,C#,Datatable,Datarow,我基本上是想做一个定制的datarow比较器,一个小的并且简单的东西 基本上,我试图比较datatableA中的列Mykey1,以及datatableB中的列Mykey2 我有三个错误,我不知道为什么会抛出它们或者如何更正它们。是的,我知道我正在使用for int循环并将其更改为字符串,但显然这只是一个实验室,是的,我将比较字符串 这里是错误 无法将类型“int”隐式转换为“string” ConsoleApplication2.MyDataRowComparer“未实现接口成员”System.

我基本上是想做一个定制的datarow比较器,一个小的并且简单的东西

基本上,我试图比较datatableA中的列
Mykey1
,以及datatableB中的列
Mykey2

我有三个错误,我不知道为什么会抛出它们或者如何更正它们。是的,我知道我正在使用for int循环并将其更改为字符串,但显然这只是一个实验室,是的,我将比较字符串

这里是错误

  • 无法将类型“int”隐式转换为“string”
  • ConsoleApplication2.MyDataRowComparer“未实现接口成员”System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)
  • ConsoleApplication2.MyDataRowComparer“未实现接口成员”System.Collections.Generic.IEqualityComparer.Equals(System.Data.DataRow,System.Data.DataRow)
  • 这是密码

    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"));
            }
        }
    }
    

    String.Compare
    返回一个整数而不是字符串,因此需要更改比较方法签名。 这将解决第一个错误


    其他错误是由于类中缺少所需的实现,因为错误表明缺少
    System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)
    方法和
    System.Collections.Generic.IEqualityComparer.Equals(System.Data.DataRow,System.Data.DataRow)
    method.

    +1和
    MyDataRowComparer.Compare
    将不会被调用,因为它不是接口
    IEqualityComparer
    的一部分。谢谢,请查看我的编辑,关于我为什么不获取任何行的任何想法如果您能说出哪一行产生错误,这将非常有用。我能看到的唯一能产生它的地方是当你试图获得PrimaryKey时,我会检查一下。 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; } }