Querying

How to query existing files

Once we have some files in the In-Memory database, via either uploading them or intiializing them, it might be useful to search / query existing files (for example, to construct assertions that a given file was uploaded or updated or deleted).

You need to know that there are several methods we can use.

Please check them below.

GetFileById

If you know the unique identifier of the file (this Id is stored in the column property and also returned in the CommitFileBlocksRequest), then you can retrieve it directly with this method.

var _fileDb = (_context as XrmFakedContext).FileDb;
var singleFile = _fileDb.GetFileById(_file.Id);

In the above example you’ll get a FileAttachment instance with information about the file and its contents.

GetAllFiles

This method will return all the file attatchments currently in the in memory database.

var _fileDb = (_context as XrmFakedContext).FileDb;
var lisOfFiles = _fileDb.GetAllFiles();

CreateQuery

This method is useful for searching as it returns an IQueryable of FileAttachment(s).

var _fileDb = (_context as XrmFakedContext).FileDb;
var filteredFiles = _fileDb.CreateQuery()
                        .Where(f => f.AttributeName == "dv_file")
                        .Tolist();

The above example will return all files uploaded against an attribute name of “dv_file” across all records.