I have 3 List<> objects.
List<T> listA = new List<t>();
List<T> listB = new List<t>();
List<T> listC = new List<t>();
I need to unite listA and ListB and assign it to ListC:
List<T> listC = new ListA +ListB;
My question is how to implement it?
I have 3 List<> objects.
List<T> listA = new List<t>();
List<T> listB = new List<t>();
List<T> listC = new List<t>();
I need to unite listA and ListB and assign it to ListC:
List<T> listC = new ListA +ListB;
My question is how to implement it?
List<int> A = new List<int>();
A.Add(1);
A.Add(2);
List<int> B = new List<int>();
B.Add(3);
B.Add(4);
List<int> C = new List<int>();
C = A.Union<int>(B).ToList<int>();
Try AddRange() this:-
listC.AddRange(listA);
listC.AddRange(listB);
Kinda depends on your requirements, but might be as easy as
listC.AddRange(listA);
listC.AddRange(listB);
Either use AddRange as others have already mentioned or , if you want to union both as mentioned, you need to provide a IEqualityComparer<T> and use Enumerable.Union:
List<Foo> third = first.Union(second, new FooComparer()).ToList();
Here's an examplary implementation:
public class FooComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo x, Foo y)
{
return x.Id == y.Id; // assuming that there's a value type like ID as key
}
public int GetHashCode(Foo obj)
{
return obj.Id.GetHashCode();
}
}
Union returns only unique values.