How to merge 2 IList collections?

  • Thread starter Thread starter David Shultz
  • Start date Start date
D

David Shultz

Can anyone show me how to cleanly merge 2 IList collections? Keeping
only unique items from the 2 lists?

--David
 
Hi David,
Can anyone show me how to cleanly merge 2 IList collections? Keeping
only unique items from the 2 lists?

Let's assume List1 is the first list, List2 the second:

=======
foreach ( object o in List2 )
if ( !List1.Contains( o ) )
List1.Add( o );
=======

Regards,

Frank Eller
 
Take advantage of Linq and use


foreach (var item in List2.Except(List1))
{
List11.Add(item);
}
 
Back
Top