比较数据表并保存第三个数据表中不匹配的行

时间:2015-05-22 17:30:36

标签: c# asp.net linq datatable

我正在尝试比较两个数据表并捕获第三个数据表中的差异。

DataTable one = new DataTable();
one.Columns.Add("ID");
one.Columns.Add("PCT");
one.Rows.Add("1", "0.1");
one.Rows.Add("2", "0.2");
one.Rows.Add("3", "0.3");
DataTable two = new DataTable();
two.Columns.Add("ID");
two.Columns.Add("PCT");
two.Columns.Add("OldPCT");
two.Rows.Add("1", "0.1", "0");
two.Rows.Add("2", "0.1", "0");
two.Rows.Add("3", "0.9", "0");
two.Columns.Remove("OldPCT");

//First method
DataTable three = two.AsEnumerable().Except(one.AsEnumerable()).CopyToDataTable();
foreach (DataRow dr in three.AsEnumerable())
{
    string strID = dr[0].ToString();
    string strPCT = dr[1].ToString();
}

//second method
var diffName = two.AsEnumerable().Select(r => r.Field<string>("PCT")).Except(one.AsEnumerable().Select(r => r.Field<string>("PCT")));
if (diffName.Any())
{
    DataTable Table3 = (from row in two.AsEnumerable()
                        join name in diffName
                        on row.Field<string>("PCT") equals name
                        select row).CopyToDataTable();
}

到目前为止,我已经尝试了两种方法,但我没有达到预期的结果,应该是这样。

在第三个数据表中,值应如下所述。

 ID    PCT
 2     O.1
 3     0.9   

最近的一个:

DataTable one = new DataTable();
one.Columns.Add("ID");
one.Columns.Add("PCT");
one.Rows.Add("1", "0.1");
one.Rows.Add("2", "0.2");
one.Rows.Add("2", "0.2");
one.Rows.Add("3", "0.3");
one.Rows.Add("3", "0.3");
DataTable two = new DataTable();
two.Columns.Add("ID");
two.Columns.Add("PCT");
two.Rows.Add("1", "0.1");
two.Rows.Add("2", "0.1");
two.Rows.Add("2", "0.1");
two.Rows.Add("3", "0.8");
two.Rows.Add("3", "0.9");

现在我需要从第一行获取数据表2中的所有行。但我只得到最后三排。

2 个答案:

答案 0 :(得分:3)

基于Hogan的答案,您可以使用DataRowComparer.Default作为Except()方法的第二个参数(而不是创建自定义IEqualityComparer):

// this will get all rows from table two that don't match rows in one
// the result is an IEnumerable<DataRow> 
var unmatched = two.AsEnumerable()
  .Except(one.AsEnumerable(), DataRowComparer.Default);

// CopyToDataTable converts an IEnumerable<DataRow> into a DataTable
// but it blows up if the source object is empty

// this statement makes sure unmatched has data before calling CopyToDataTable()
// if it is empty, we 'clone' (make an empty copy) of one of the original DataTables
var three = unmatched.Any() ? unmatched.CopyToDataTable() : one.Clone();

这将对每行中的字段进行基于值的比较,以确定它们是否相等。

答案 1 :(得分:1)

您需要自定义IEqualityComparer

set myval [HOWTOEVAL $myref]