I have already thought about how I’m going to solve this by rolling my own solution, but I wondered if .NET already has the functionality for what I’m trying to acheive – if so, I’d rather use something built-in.
Suppose I have two instances of a Widget object, let’s call them PartA and PartB. The information from each has been garnered from two different web services, but both have matching IDs.
PartA
{
ID: 19,
name: "Percy",
taste: "",
colour: "Blue",
shape: "",
same_same: "but different"
}
PartB
{
ID: 19,
name: "",
taste: "Sweet",
colour: "",
shape: "Hexagon",
same_same: "but not the same"
}
I want to merge these to create the following:
Result
{
ID: 19,
name: "Percy",
taste: "Sweet",
colour: "Blue",
shape: "Hexagon",
same_same: "but different"
}
Notice how the value for same_same differs between each, but we consider PartA the master, so the result retains the value but different.
Now to complicate matters:
Suppose we have two lists:
List<Widget> PartA = getPartA();
List<Widget> PartB = getPartB();
Now here’s some pseudocode describing what I want to do:
List<Widget> Result = PartA.MergeWith(PartB).MergeObjectsOn(Widget.ID).toList();
You could write your own extension method(s), something like this:
Where
ICanMergeis like:Implemented e.g. like:
Then it’s as simple as
PartA.MergeWith(PartB).ToList().