16

我有两个数据表,我想从第一个中选择第二个中不存在的行

例如:

表 A
  标识列
  1 个数据1
  2 数据2
  3 数据3
  4 数据4

表 B
  标识列
  1 个数据10
  3 数据30

我希望结果是:

表 C
  标识列
  2 数据2
  4 数据4
4

3 回答 3

18

您可以使用 Linq,尤其Enumerable.Except有助于在 TableA 中查找不在 TableB 中的 id:

var idsNotInB = TableA.AsEnumerable().Select(r => r.Field<int>("id"))
        .Except(TableB.AsEnumerable().Select(r => r.Field<int>("id")));
DataTable TableC = (from row in TableA.AsEnumerable()
                   join id in idsNotInB 
                   on row.Field<int>("id") equals id
                   select row).CopyToDataTable();

您也可以使用Where,但效率会降低:

DataTable TableC = TableA.AsEnumerable()
    .Where(ra =>  !TableB.AsEnumerable()
                        .Any(rb => rb.Field<int>("id") == ra.Field<int>("id")))
    .CopyToDataTable();
于 2013-03-30T00:05:03.743 回答
4

我得到了一个没有 LINQ 的解决方案:

public DataTable CompareDataTables(DataTable first, DataTable second)
{
    first.TableName = "FirstTable";
    second.TableName = "SecondTable";

    //Create Empty Table
    DataTable table = new DataTable("Difference");

    try
    {
        //Must use a Dataset to make use of a DataRelation object
        using (DataSet ds = new DataSet())
        {
            //Add tables
            ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });

            //Get Columns for DataRelation
            DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];

            for (int i = 0; i < firstcolumns.Length; i++)
            {
                firstcolumns[i] = ds.Tables[0].Columns[i];
            }

            DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

            for (int i = 0; i < secondcolumns.Length; i++)
            {
                secondcolumns[i] = ds.Tables[1].Columns[i];
            }

            //Create DataRelation
            DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

            ds.Relations.Add(r);

            //Create columns for return table
            for (int i = 0; i < first.Columns.Count; i++)
            {
                table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
            }

            //If First Row not in Second, Add to return table.
            table.BeginLoadData();

            foreach (DataRow parentrow in ds.Tables[0].Rows)
            {
                DataRow[] childrows = parentrow.GetChildRows(r);
                if (childrows == null || childrows.Length == 0)
                    table.LoadDataRow(parentrow.ItemArray, true);
            }

            table.EndLoadData();

        }
    }
}

更多信息请访问http://microsoftdotnetsolutions.blogspot.in/2012/12/compare-two-datatables.html

于 2013-07-09T05:52:07.020 回答
1

You can use Linq Enumerable.Except Method function to get diffence between two DataTable's Here i use firstDt and secondDt,remember both Dt's have the same structure.

 var EntriesNotInB = firstDt.AsEnumerable().Select(r => r.Field<string>("abc")).Except(secondDt.AsEnumerable().Select(r => r.Field<string>("abc")));

        if (EntriesNotInB.Count() > 0)
        {
            DataTable dt = (from row in firstDt.AsEnumerable()join id in EntriesNotInB  on row.Field<string>("abc") equals id select row).CopyToDataTable();
            foreach (DataRow row in dt.Rows)
            {
              /////Place your code to manipulate on datatable Rows
            }
        }

To read more on Enumerable.Except Method,Go to http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except(v=vs.110).aspx

and its Done!!!! Happy Coding.........

于 2013-12-20T10:14:26.560 回答