This is a little new to me. I was asked to write an ETL program that loads two sets of data into the same table. Dataset No. 1 is complete and contains all the data for the table. However, data set No. 2 contains only the changes that need to be applied to the first data set. Note:
// Dataset No. 1: widget table
+----+------+------+------+------+ | ID | COL1 | COL2 | COL3 | COL4 | +----+------+------+------+------+ | 1 | abcd | abcd | abcd | abcd | +----+------+------+------+------+ | 2 | abcd | abcd | abcd | abcd | +----+------+------+------+------+
// Dataset # 2: Widgets_Changes table
+----+------+------+------+------+ | ID | COL1 | COL2 | COL3 | COL4 | +----+------+------+------+------+ | 1 | | efgh | | ijkl | +----+------+------+------+------+ | 2 | mnop | | qrst | | +----+------+------+------+------+
// Expected Result: Widgets with all changes
+----+------+------+------+------+ | ID | COL1 | COL2 | COL3 | COL4 | +----+------+------+------+------+ | 1 | abcd | efgj | abcd | ijkl | +----+------+------+------+------+ | 2 | mnop | abcd | qrst | abcd | +----+------+------+------+------+
The obvious approach (which I am trying to avoid) is to pull each widget from the first table and compare the properties by properties:
// Simplified example: using ( var db = new MyEntityDatabase() ){ var widget = from p in db.Widgets select p where p.ID == 1; var widget_diff = from p in db.Widgets_Changes select p where p.ID == 1 widget.COL1 = widget_diff.COL1 ?? widget.COL1; widget.COL2 = widget_diff.COL2 ?? widget.COL2; widget.COL3 = widget_diff.COL3 ?? widget.COL3; // ...etc db.saveChanges(); }
However, in this particular data set there are more than 200 fields with a large number of input files that adhere to the same methodology (a complete data set accompanied by a diff data set), but have a completely different scheme. Obviously, I would rather have something portable so that I can just run the files, rather than matching properties by properties for each data set.
Is there a way in which I can iterate over the properties of both objects and update values that are not null?
source share