我如何使用LINQ从各种子字符串开始过滤掉字符串?

Pro*_*ofK 3 .net c# linq

假设我有var lines = IEnumerable<string>,并且lines包含各种行,其中第一个1..n字符将它们从进程中排除.例如以'*','Eg','Sample'等开头的行.

排除令牌列表是可变的,仅在运行时才知道

lines.Where(l => !l.StartsWith("*") && !l.StartsWith("E.g.") && ...
Run Code Online (Sandbox Code Playgroud)

变得有些问题.

我怎么能实现这个目标?

Bla*_*hma 8

使用LINQ:

 List<string> exceptions = new List<string>() { "AA", "EE" };

 List<string> lines = new List<string>() { "Hello", "AAHello", "BHello", "EEHello" };

 var result = lines.Where(x => !exceptions.Any(e => x.StartsWith(e))).ToList();
 // Returns only "Hello", "BHello"
Run Code Online (Sandbox Code Playgroud)