Query Dynamic File System Information Using LINQ

I need to return a list of files matching some dynamic criteria. I tried to do this using LINQ.

I found that you can use dynamic LINQ using the System.Linq.Dynamic namespace, which is mentioned in Scott Gug’s Blog .

But I'm not sure if I can use what I need.

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);

Now I need to be able to filter these files using some dynamic filters created by the user. e.g. Extension = .txt

Can someone point me in the right direction?

Thank. Martin.

EDIT:

An 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)");

. .

+3
3

, .

var diSource = new DirectoryInfo(SourceLocation);            
var files = GetFilesRecursive(diSource);
var matches = files.AsQueryable().Where(sFilter, oParams);

SourceLocation - , . GetFileRecursive - .

sFilter , , oParams - . , .

+3
var files = fileList.Where(
    f => f.Modified > DateAdd(DateInterval.Days, -1, DateTime.Now);

var files = fileList.Where(f => f.Name.EndsWith(".txt"));

var files = fileList.Where(f => f.IsReadOnly);

LINQ. f => f. , . . , FileInfo, true, . ...

var files = fileList.Where(f => MyFunction(f));

, .

+2

If you are not using the fileinfo properties as part of your filtering, you can simply use the following:

IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles(**"*.txt"**,
 System.IO.SearchOption.AllDirectories);
0
source

Source: https://habr.com/ru/post/1753713/


All Articles