C#在做一个简单的数据库比较器时发生错误 [英] C# Errors when doing a simple datarow comparer
问题描述
我基本上是试图做一个自定义的数据记录比较器,一些小的东西,并保持简单。
I'm basically trying to do a custom datarow comparer, something small and to keep it simple.
基本上我试图比较列<$ c $在datatableA和 Mykey2
中,在datatableB中的c> Mykey1
Basically i'm trying to compare column Mykey1
in datatableA and Mykey2
in datatableB.
错误,我不知道为什么他们被抛出或如何纠正它们。是的,我知道我正在使用一个int循环并将其更改为一个字符串,但显然这只是一个实验室,是的,我会比较字符串。
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.
- 不能将类型'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"));
}
}
}
编辑
这是我的新DataRowComparer,但是它没有返回任何结果,而是获得
EDIT
Here is my new DataRowComparer, however it's not returning any results instead i get
{索引不在数组的边界。}
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;
}
}
推荐答案
比较
返回整数而不是字符串,因此您需要更改Compare方法签名。
这应该会解决第一个错误。
String.Compare
returns an integer and not a string so you need to change your Compare method signature.
That should resolve the first error.
其余的错误是由于您的类中缺少所需的实现,因为错误提示您缺少 System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)
方法和 System.Collections.Generic.IEqualityComparer.Equals (System.Data.DataRow,System.Data.DataRow)
方法。
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.
这篇关于C#在做一个简单的数据库比较器时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!