Exclude list items that contain values from another list

lekso picture lekso · Jun 28, 2012 · Viewed 35.6k times · Source

There are two lists:

List<string> excluded = new List<string>() { ".pdf", ".jpg" };
List<string> dataset = new List<string>() {"valid string", "invalid string.pdf", "invalid string2.jpg","valid string 2.xml" };

How can I filter-out values from the "dataset" list which contain any keyword from the "excluded" list?

Answer

MarcinJuraszek picture MarcinJuraszek · Jun 28, 2012
var results = dataset.Where(i => !excluded.Any(e => i.Contains(e)));