For List
s I could use the Union
method:
var finalCollection = new List<MyType>();
var list1= new List<MyType>();
var list2 = new List<MyType>();
finalCollection = list1.Union(list2).ToList();
But when I try to do the same with ConcurrentBag
s,
var finalCollection = new ConcurrentBag<MyType>();
var bag1= new ConcurrentBag<MyType>();
var bag2= new ConcurrentBag<MyType>();
finalCollection = bag1.Union(bag2);
I get:
Cannot implicitly convert type "System.Collections.Generic.IEnumerable< MyType>" to "System.Collections.Concurrent.ConcurrentBag"
Perhaps the problem is you are trying to Union the elements into a new
ConcurrentBag
What you actually get after the union is an
IEnumerable
ofMyType
To get back a
ConcurrentBag
try doing it like so: