在C#中,如何使用另一个列表的StartsWith()条件过滤列表?

leo*_*ora 7 c# linq collections

假设我有一个字符串列表:

var searchList = new List<string>();
searchList.Add("Joe"):
searchList.Add("Bob");
Run Code Online (Sandbox Code Playgroud)

我有另一个清单

var fullNameList = new List<string>();
fullNameList.Add("Joe Thompson"):
fullNameList.Add("Bob Jones");
fullNameList.Add("Bob Smith");
fullNameList.Add("Billy Smith");
fullNameList.Add("Joe Williams");
Run Code Online (Sandbox Code Playgroud)

我现在想根据它是否存在于搜索列表中来过滤fullNameList,但我没有进行直接匹配,而是"匹配".所以在我的例子中,过滤后我希望这是结果

"Joe Thompson"
"Bob Jones"
"Bob Smith"
"Joe Williams"
Run Code Online (Sandbox Code Playgroud)

(你可以看到从比利开始的记录没有包括在内,因为它没有从搜索列表中的任何项目开始)

我可以像这样"手动"执行此操作:

 var resultList = fullNameList.Where(r=> r.StartsWith("Joe") || r.StartsWith("Bob"));
Run Code Online (Sandbox Code Playgroud)

但我希望它是动态的(因为将来可能会在搜索列表中添加更多项目).

我可以在这样的循环中执行此操作:

 var resultsList = new List<string>();

 foreach (var item in searchList)
 { 
      resultsList.AddRange(fullNameList.Where(r=>r.StartsWith(item));
 } 
Run Code Online (Sandbox Code Playgroud)

但是想看看是否还有一个列表上的Startswith而不是单行linq代码中的单个字符串?

ASh*_*ASh 13

var resultList = fullNameList.Where(r => searchList.Any(f=>r.StartsWith(f)));
Run Code Online (Sandbox Code Playgroud)