有两个列表:
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" };
如何从“数据集”列表中过滤掉包含“排除”列表中的任何关键字的值?
var results = dataset.Where(i => !excluded.Any(e => i.Contains(e)));
// Contains four values.
int[] values1 = { 1, 2, 3, 4 };
// Contains three values (1 and 2 also found in values1).
int[] values2 = { 1, 2, 5 };
// Remove all values2 from values1.
var result = values1.Except(values2);
尝试:
var result = from s in dataset
from e in excluded
where !s.Contains(e)
select e;
var result=dataset.Where(x=>!excluded.Exists(y=>x.Contains(y)));
这在排除列表为空时也有效。
var result = dataset.Where(x => !excluded.Contains(x));