I wrote an application in C# and the way I write queries is inefficient.
For example, I have 3 entities :
Revision, Page, PageLine
Each revision contains list of pages and each page contains list of page lines.
To retrieve a specific revision, I retrieve all revisions from the DB and perform the filtering locally.
Right now everything is working fine, but assuming the DB will be filled with a lot of data, the queries will be slow and inefficient.
//ViewModel
private async void GetRevisionData(int revisionId)
{
List<Page> eepromPages = revisionEEPROMDataService.GetEEPROMPages(revisionId).Result;;
}
//DataService
public async Task<List<Page>> GetEEPROMPages(int eepromRevId)
{
string[] includes = { "Pages", "Pages.PageLines" };
IEnumerable<RevisionEEPROM> list = (IEnumerable<RevisionEEPROM>)await dataService.GetAll(includes);
return list.Where(r => r.Id == eepromRevId).SelectMany(p => p.Pages).ToList();
}
//GenericDataService
public async Task<IEnumerable<T>> GetAll(string[] includes = null)
{
using (DeployToolDBContex contex = _contexFactory.CreateDbContext())
{
if (includes != null)
{
var query = contex.Set<T>().AsQueryable();
foreach (var include in includes)
query = query.Include(include);
return query.ToList();
}
else
{
IEnumerable<T> entities = await contex.Set<T>().ToListAsync();
return entities;
}
}
}
How can I make the query efficient and retrieve only the relevant data?