问题
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.
- Cannot implicitly convert type 'int' to 'string'
- ConsoleApplication2.MyDataRowComparer' does not implement interface member 'System.Collections.Generic.IEqualityComparer.GetHashCode(System.Data.DataRow)
- 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