First of all, apologize if this is a blatant duplicate, I was looking for all phrases that could give a result, but I struggled to find an answer / construct search terms coherently.
I have collection 1 and collection 2 of object A.
Date
Type
Value
Collection 1 covers dates 1 to 2 for type 2, and collection 2 covers dates 3 to 4 for type 1:
[0] = 1st, TypeA, 3
[1] = 2nd, TypeA, 3
[2] = 1st, TypeB, 57
[3] = 2nd, TypeB, 57
[0] = 3rd, TypeB, 57
[1] = 4th, TypeB, 58
My initial requirement was to combine the two collections. No problem:
var result = collection1.Union(collection2);
However, now I would like to combine both collections, but excluding objects from Collection 1, where the Type does not exist in Collection 2:
[0] = 1st, TypeB, 57
[1] = 2nd, TypeB, 57
[2] = 3rd, TypeB, 57
[3] = 4th, TypeB, 58
I'm sure there is a simple (hopefully linq) single line solution, but I just can't make my brain understand what it is.
The best (two lines) I've got so far:
var typesToInclude = collection2.GroupBy(c => c.Type).Select(group => group.First().Type);
var result = collection2.Union(collection1.Where(c => typesToInclude.Contains(c.Type)));
: (