C#Linq删除列表中包含的字符串[]中包含的记录

Ali*_*san 4 c# linq

现在我正在做这样的事情来删除myList中的单词,这是正常的,

List<string> myList = matches
.Cast<Match>()
.Select(m => m.Value)
.Distinct()
.ToList();                                                                       

myList.RemoveAll((x) => x.Contains("word1") 
|| x.Contains("word1")
|| x.Contains("word2")
|| x.Contains("word3")
|| x.StartsWith("D")
);

string[] ab = new string[] { "word1", "word2", "word3" };   
Run Code Online (Sandbox Code Playgroud)

但现在我想提供一个字符串[]列表而不是添加x.Contains("blah blah")其次我还想将两个语句合并为一个,使其成为单个linq查询.

Pau*_*ane 6

Enumerable.Except是您过滤掉项目的朋友.你需要做一个决赛Where来处理这个StartsWith案子.

IEnumerable<string> filtered = myList.Except(ab);
Run Code Online (Sandbox Code Playgroud)

所以完整:

IEnumerable<string> myList = matches.Select(_ => _.Value)
                                    .Distinct()
                                    .Except(new [] { "word1", "word2", "word3" })
                                    .Where(_ => !_.StartsWith("D"));
Run Code Online (Sandbox Code Playgroud)