I have a method who merge two lists. The two merged lists are lists of subtypes objects of the returned list. By “merging” I mean “Allowing duplicates”.
So Sub1 and Sub2 types are subtypes from Sup1 type.
Here is my code
var listSub1 = new List<Sub1>(); //With some content added..
var listSub2 = new List<Sub2>(); //With content too..
var listToReturn = new List<Sup1>();
listToReturn.AddRange(listSub1.Select(item => item as Sup1).ToList());
listToReturn.AddRange(listSub2.Select(item => item as Sup1).ToList());
return listeToReturn;
It is working fine but I wonder if it is the best way to merge and cast the lists.
you could write
Note that
Unionis not correct to be used, since it will make sure that objects that are “equal” will come out only once.