I would like to ask whether there's an elegant and efficient way to merge two lists of MyClass into one?
MyClass looks like this:
- ID:
int
- Name:
string
- ExtID:
int?
and the lists are populated from different sources and objects in lists do share ID, so it looks like that:
MyClass instance from List1
ID = someInt
Name = someString
ExtID = null
And MyClass instance from List2
ID = someInt (same as List1)
Name = someString (same as List1)
ExtID = someInt
What I basically need is to combine these two lists, so the outcome is a list containing:
ID = someInt (from List1)
Name = someString (from List1)
ExtID = someInt (null if no corresponding item - based on ID - on List2)
I know I can do this simply using foreach loop, but I'd love to know if there's more elegant and maybe preferred (due to performance, readability) method?
Merge
function that combines two instances with the sameID
into one - then you can concat the lists, group by id and finally fold/reduce over the groups using your merge function - that's how I would do it for readability - for performance you are probably fine with sorting and a loop