Assuming your objects are of the same type, you can use either Union
or Concat
. Note that, like the SQL UNION
keyword, the Union
operation will ensure that duplicates are eliminated, whereas Concat
(like UNION ALL
) will simply add the second list to the end of the first.
IEnumerable<T> first = ...;
IEnumerable<T> second = ...;
IEnumerable<T> combined = first.Concat(second);
or
IEnumerable<T> combined = first.Union(second);
If they are of different types, then you'll have to Select
them into something common. For example:
IEnumerable<TOne> first = ...;
IEnumerable<TTwo> second = ...;
IEnumerable<T> combined = first.Select(f => ConvertToT(f)).Concat(
second.Select(s => ConvertToT(s)));
Where ConvertToT(TOne f)
and ConvertToT(TTwo s)
represent an operation that somehow converts an instance of TOne
(and TTwo
, respectively) into an instance of T
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…