views:

28

answers:

3
listSuper
listSub_A
listSub_B

Is there any extension methods that replace the following piece of code?

 foreach(int a in listSuper)
 {
 if (!listSub_A.Contains(a))
 {
 listSub_B.Add(a);
 }
 }

In short I want to fill listSub_B with elements in listSuper which are not in listSub_A.

+4  A: 
listSub_B.AddRange(listSuper.Except(listSub_A));
Marc Gravell
+3  A: 
listSub_B.AddRange(listSuper.Except(listSub_A));
Andrey
+1  A: 

Why not have a property rather than an extension method since you wont need to update listSub_B if listSub_A changes?

public ...  listSub_B
{
  get{return listSuper.Except(listSub_A);}
}
Vivek