有两个列表:
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" };
我该如何从"dataset"列表中过滤掉包含"excluded"列表中任意关键词的值?
var results = dataset.Where(i => !excluded.Any(e => i.Contains(e)));
excluded
是包含要排除项的列表的名称。 - SvenAelterman// 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));
变量结果 = 数据集.筛选(x => !排除包含(x));
excluded
很大,将其定义为HashSet<string>
。 - Jodrell