I need to be able to return a list of files that meet some dynamic criteria. I’ve tried to do this using LINQ.
I’ve found that it is possible to use dynamic LINQ using the System.Linq.Dynamic namespace that it is mentioned in Scott Gu’s Blog.
But I’m not sure if can be used for what I need it for.
So far I get all the files but I’m not sure where to go from there.
// Take a snapshot of the file system.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(SourceLocation);
// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
I now need to be able to filter these files down, using some dynamic filters that the user has created. e.g. Extension = .txt
Can anyone point me in the right direction?
Thanks.
Martin.
EDIT:
The example in the Dynamic Linq library looks like this :
var query =
db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("New(CompanyName as Name, Phone)");
I was hoping to adapt this for the filesystem. So I can just build up a filter string and use that.
This is the method that I’ve used.
SourceLocation is a just a file path to the directory that I wish to search. GetFileRecursive is just a custom method to return all the files in the child folders.
I build the sFilter parameter based on the filters that my users have defined, and oParams is an array of the values for these filters. This allows my users to dynamically define as many filters as they require.