Saturday, June 28, 2014

what is a good way to check (can I use a Lambda =>) if an item is contained in a List of values?

If you want to get a list of all items that are in both lists, you could use the Intersect method:



List<string> lstA = new List<string>(new string[] { "a", "b", "c", "d" });
List<string> lstB = new List<string>(new string[] { "b", "c" });

List<string> list = lstB.Intersect<string>(lstA).ToList(); // will contain b and c
//do anything with the items in "list":
list.ForEach(s => { /* do something with the string...*/ });


No comments:

Post a Comment