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

前端 未结 2 635
悲哀的现实
悲哀的现实 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: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(){".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);
    

提交回复
热议问题