根据一列获得两个数据表之间的差异 [英] Get difference between two data-tables according to one column

查看:137
本文介绍了根据一列获得两个数据表之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情形:

这有50条记录,并有2个记录。表B表A

Table A that has 50 records and Table B that has 2 records.

我需要定义一个新表,说TableDiff其中应包含表A共有48条记录,这并不表B中存在

I need to define a new table, say TableDiff which should contain 48 records from Table A that doesn't exist in Table B

我的问题是表A和B表是不相同的,但我有它存在,我需要用它来比较两个表中的字段 ROWID

My problem is that Table A and Table B are not identical but I have the field rowId which exists in both tables that I need to compare using it.

推荐答案

使用的一种方法 Enumerable.Except Enumerable.Join

var aIDs = TableA.AsEnumerable().Select(r => r.Field<int>("RowID"));
var bIDs = TableB.AsEnumerable().Select(r => r.Field<int>("RowID"));
var diff = aIDs.Except(bIDs);
DataTable tblDiff = (from r in TableA.AsEnumerable()
                    join dId in diff on r.Field<int>("RowID") equals dId
                    select r).CopyToDataTable();

这里的LINQ到对象左加入-approach:

Here's the linq-to-objects "left-join"-approach:

DataTable tblDiff = (from rA in TableA.AsEnumerable()
                     join rB in TableB.AsEnumerable()
                     on rA.Field<int>("RowID") equals rB.Field<int>("RowID") into joinedRows
                     from ab in joinedRows.DefaultIfEmpty()
                     where ab == null
                     select rA).CopyToDataTable();

这篇关于根据一列获得两个数据表之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆