How to write a LINQ statement to get files with specific extensions from a dynamic list?

前端 未结 2 636
悲哀的现实
悲哀的现实 2021-01-28 12:36

I have a list of dynamic objects

List myDynamicList;

where one item would look similar to below:

dynamic d = ne         


        
相关标签:
2条回答
  • 2021-01-28 13:25
    web.config
    
    
    <configuration>
       ....
       <appSettings>
          <add key="ExtensionArray" value="dll,exe"/>
       </appSettings>
       ....
    </configuration>
    
    
    C# snippet
    
    string ExtensionArray== WebConfigurationManager.AppSettings["Extension"];
    
    var list=ExtensionArray.Split(',').ToList<string>();
    
    var libraries = myDynamicList.Where(d => list.Any(e=>e==Path.GetExtension(d.FileName)))
                                  .Select(d => d.FileName);
    
    0 讨论(0)
  • 2021-01-28 13:34

    You can use a list to save all the extension that you need and use Contains extension method in your query:

    var list=new List<string>(){".dll", ".exe"};
    var libraries = myDynamicList.Where(d => list.Contains(Path.GetExtension(d.FileName)))
                                 .Select(d => d.FileName);
    

    You can also use Any extension method:

     var libraries = myDynamicList.Where(d => list.Any(e=>e==Path.GetExtension(d.FileName)))
                                  .Select(d => d.FileName);
    
    0 讨论(0)
提交回复
热议问题