C# 从另一个列表中排除包含值的列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11241210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:05:57  来源:igfitidea点击:

Exclude list items that contain values from another list

c#.netlinqfiltering

提问by lekso

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?

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

采纳答案by MarcinJuraszek

var results = dataset.Where(i => !excluded.Any(e => i.Contains(e)));

回答by abatishchev

Try:

尝试:

var result = from s in dataset
             from e in excluded 
             where !s.Contains(e)
             select e;

回答by M_Farahmand

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

This also works when excluded list is empty.

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

回答by Majid Shahmohammadi

// 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

https://www.dotnetperls.com/except

回答by Kyle Zhao

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

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