Linq filter List where it contains a string value from another List

前端 未结 3 450
盖世英雄少女心
盖世英雄少女心 2020-12-14 15:31

I have 2 List objects (simplified):

var fileList = Directory.EnumerateFiles(baseSourceFolderStr, fileNameStartStr + \"*\", SearchOption.AllDirectories);

var         


        
相关标签:
3条回答
  • 2020-12-14 15:54

    its even easier:

    fileList.Where(item => filterList.Contains(item))
    

    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())));
    
    0 讨论(0)
  • 2020-12-14 15:59

    Try the following:

    var filteredFileSet = fileList.Where(item => filterList.Contains(item));
    

    When you iterate over filteredFileSet (See LINQ Execution) it will consist of a set of IEnumberable values. This is based on the Where Operator checking to ensure that items within the fileList data set are contained within the filterList set.

    As fileList is an IEnumerable set of string values, you can pass the 'item' value directly into the Contains method.

    0 讨论(0)
  • 2020-12-14 16:18

    you can do that

    var filteredFileList = fileList.Where(fl => filterList.Contains(fl.ToString()));
    
    0 讨论(0)
提交回复
热议问题