28

有两个列表:

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" };

如何从“数据集”列表中过滤掉包含“排除”列表中的任何关键字的值?

4

5 回答 5

36
var results = dataset.Where(i => !excluded.Any(e => i.Contains(e)));
于 2012-06-28T09:01:24.017 回答
12
// 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);

https://www.dotnetperls.com/except

于 2019-07-20T09:57:16.290 回答
7

尝试:

var result = from s in dataset
             from e in excluded 
             where !s.Contains(e)
             select e;
于 2012-06-28T09:03:50.253 回答
0
var result=dataset.Where(x=>!excluded.Exists(y=>x.Contains(y)));

这在排除列表为空时也有效。

于 2017-07-19T12:16:14.843 回答
-1

var result = dataset.Where(x => !excluded.Contains(x));

于 2019-08-01T17:52:46.603 回答