开发者

Compare two similar but not identical data tables

开发者 https://www.devze.com 2023-03-21 14:20 出处:网络
I have two data tables which have a primary key column in common but otherwise different structures as they\'re created from different tables. I\'d like to check which rows are missing from data table

I have two data tables which have a primary key column in common but otherwise different structures as they're created from different tables. I'd like to check which rows are missing from data table B compared to A, with the aim of a开发者_如何学运维dding in the missing rows to the database.

I've had a look at questions like this one, which provide good answers but the data tables are usually assumed to be the same.

Since I want to get a list of new rows for inserting, is there any way I can do this without iterating through the rows and checking the PKs? If I'm not mistaken, Merge will keep the old rows, which I don't want to resubmit to the database.

Both tables will have only a few dozen rows, a couple of hundred maximum. Would the computational hit of doing it row-by-row be negligible anyway?

edit: the two source tables are on different databases/servers, so I can't easily do the comparison in SQL. I'd prefer the consuming application to do it anyway, as I may add some more row processing before I'm done.


I hope I've understood your needs correctly, but how about using Linq? Load the keys of your 2 tables into 2 IEnumerable<keytype> objects, e.g. (assuming your keys are of type int)

IEnumerable<int> keys1, keys2;
// code to load the keys into these collections
// to find the keys in keys1 that are missing from keys2:
var missingKeys = keys1.Where(k1 => !keys2.Any(k2 => k2 == k1)); 

HTH


Is it not easier/quicker to do this in SQL e.g:

SELECT col1 FROM table1
MINUS
SELECT col1 FROM table2
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号