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

时间:2014-10-14 12:55:42

标签: c# datatable datarow

我基本上是在尝试做一个自定义数据流比较器,这个比较小并且保持简单。

基本上我正在尝试比较datatableA中的列Mykey1和datatableB中的Mykey2

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

这里有错误。

  1. 无法将类型'int'隐式转换为'string'
  2. ConsoleApplication2.MyDataRowComparer'未实现接口成员'System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)
  3. ConsoleApplication2.MyDataRowComparer'未实现接口成员'System.Collections.Generic.IEqualityComparer.Equals(System.Data.DataRow,System.Data.DataRow)
  4. 这是代码

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

    修改

    这是我的新DataRowComparer,但它没有返回任何结果,而是我得到

    {"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 个答案:

答案 0 :(得分:2)

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

其他错误是由于您在课程中缺少所需的实现,因为错误提示您缺少System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)方法和System.Collections.Generic.IEqualityComparer.Equals(System.Data.DataRow, System.Data.DataRow)方法。

相关问题