I have two lists of type KeyValuePair.
Values are as following
List A
2019-01-01 1
2019-01-02 0
2019-01-03 1
List B
2019-01-01 0
2019-01-03 1
I want to merge these two list so it looks like this
List C
2019-01-01 0 //item from list B
2019-01-02 0 //item from list A, missing date in List B
2019-01-03 1 //item from list A - items are the same in list A and B
Is there a way with Linq or MoreLinq that can actually do this, ie.
- merge items from two lists
- use item from list A if same item does not exist in list B
- replace item from list A with item from list B if they are not equal
If you know you can never have a default
DateTimeas the key:If you might have a default
DateTimethen convert the keys to aDateTime?and back again to detect the missing case reliably:Note that in both we skip the rule "replace item from list A with item from list B if they are not equal" with just "use the item from B if it exists" because we have to check if B exists anyway, and if B's value is the same as A's then it doesn't matter that we still used it.
I'd probably just build a dictionary from A and then replace the values with those from B unless I really cared about order, though.