We have two lists:
List
List
We need to keep only those entries in myList that are not contained in bannedList.
First of all, the conventional C# 1.0 approach to this problem:
List
foreach (string elem in myList )
{
if (!bannedList.Contains(elem))
{
listDistinct.Add(elem);
}
}
Now, let's use lambda expressions to arrive at a much shorter syntax.
List
listDistinct.ForEach(elem=>Console.WriteLine(elem));
The result is:
two
four
five
six
By the way, if the first statement returns a List, you can chain template methods as follows:
myList.FindAll (elem => !bannedList.Contains(elem))
.ForEach(elem=>Console.WriteLine(elem));
But the easiest approach that does not involve creating a third list is to use the RemoveAll template method:
myList.RemoveAll(elem => bannedList.Contains(elem));