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.