I'm trying improve a filter to an element list that matches any elements fron another list.
So, today, the code list looks like below:
var list1 = new List<String>();
list1.Add("One");
list1.Add("Two");
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
var newlist = list1.FindAll(l => l == "One" ||l == "Two" ).ToList();
Console.Writeline(newlist.Count);//This is the result I'm looking for.
The new requestiment is that the conditions varies depending on what is needed
So I changed l == "One" ||l == "Two"
to an array and code the logic as below:
The code changes I made is that base on
I created var cond = "One,Two,Three";
and now the code looks :
var cond = "One,Two,Three";
var list2 = new List<String>();
foreach ( String l in cond.Split(','))
{
list2.AddRange(list1.FindAll(n => n == l).ToList());
}
Console.Writeline(list2.Count);//This is the result I'm looking for.
this works, but the foreach loop with go for each conditional at the time.
Can the foreach loop be improved?
Thanks