I am using EF and Linq to return values from a database. I have a Folder
structure and a folder can contain a List of Folder
or a list of Device
. What I want is to be able to construct a list of all Device
s that sit within (or under) a folder including any folders that belong to the folder (imagine I want to see all files within a top level directory which also includes children directories).
The real kicker here is that there could be a lot of devices, so I want pagination, so ideally this would all be done with LINQ so that I can sort and paginate the query before the result set is returned.
Here is a basic version of my setup (keys, annotations and other stuff removed for simplicity)
public class Folder
{
public virtual ICollection<Folder> Children { get; set; }
public virtual ICollection<Device> Devices { get; set; }
}
// This is the function I currently have that only returns 1 folder
// needs to somehow be expanded to return devices for all folders beneath it too
function GetFolderDevices(int folderId, PaginationOptions options)
{
// Get all folders and devices
using (var dbObj = this.context.CreateDBContext())
{
EMDB.Models.Folder folder = dbObj
.AddressBook
.Include(a => a.Devices.Select(d => d.Settings))
.FirstOrDefault(f => f.FolderId == folderId);
// apply pagination here (already taken care of)
}
}