datatable找出以2个字段为主键的重复数据算法

0

0

```static string reRows() {
//初始化datatable
DataTable tblDatas = new DataTable("Datas");
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "d" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "e" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "e" });

//五条记录 ，（Version，Description）作主键，1,4重复；3,5重复。
//算法
Dictionary<string[], List<int>> dic = new Dictionary<string[], List<int>>(new EqualityComparer());
foreach (DataRow item in tblDatas.Rows)
{
string[] cur = new string[] { item["Version"].ToString(), item["Description"].ToString() };
if (dic.ContainsKey(cur)){
}
else
{
List<int> ls = new List<int>();
}
}
string result = "";
foreach (var item in dic.Values)
{
if (item.Count > 1)
{
foreach (var d in item)
{
result += d+" ";
}
result += ";";
}
}
return result;
}

public class EqualityComparer : IEqualityComparer<string[]>
{

public bool Equals(string[] x, string[] y)
{
return x[0] == y[0] && x[1] == y[1];
}

public int GetHashCode(string[] obj)
{
return 1;
}
}```
codingHeart | 园豆：1511 (小虾三级) | 2017-12-06 15:59

您需要登录以后才能回答，未注册用户请先注册