LINQ的过滤器列表&LT;字符串&GT;它包含了从另一个列表中℃的字符串值;字符串&GT; [英] Linq filter List<string> where it contains a string value from another List<string>
问题描述
我有2个List对象(简体):
I have 2 List objects (simplified):
var fileList = Directory.EnumerateFiles(baseSourceFolderStr, fileNameStartStr + "*", SearchOption.AllDirectories);
var filterList = new List<string>();
filterList.Add("ThisFolderName");
filterList.Add("ThatFolderName");
我想要过滤的fileLst返回一个包含所有从filterList文件夹名称中的文件。 (我希望是有道理的。)
I want to filter the fileLst to return only files containing any of folder names from the filterList. (I hope that makes sense..)
我曾尝试以下EX pression,但这总是返回一个空的列表。
I have tried the following expression, but this always returns an empty list.
var filteredFileList = fileList.Where(fl => fl.Any(x => filterList.Contains(x.ToString())));
我似乎无法使为什么我得到什么,显然我失去了一些东西的感觉,但我不知道。
I can't seem to make sense of why I am getting nothing, clearly I am missing something, but I have no idea what.
好了,看来我应该是在我的问题更清楚,我是想搜索文件在我的fileList从我filterList包含字符串值的字符串。我有显着低于那些谁试图做类似的事情的答案。
Ok, so it appears I should have been clearer in my question, I was trying to search for files in my fileList with a substring containing string values from my filterList. I have marked the answer below for those who are trying to do a similar thing.
推荐答案
它更容易:
fileList.Where(item => filterList.Contains(item))
如果你想没有一个确切的比赛,但对于筛选包含您可以使用此EX pression:
in case you want to filter not for an exact match but for a "contains" you can use this expression:
var t = fileList.Where(file => filterList.Any(folder => file.ToUpperInvariant().Contains(folder.ToUpperInvariant())));
这篇关于LINQ的过滤器列表&LT;字符串&GT;它包含了从另一个列表中℃的字符串值;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!