Unfortunaltly I am maintaining a system that didnt take consideration into what will happen once the system has millions of records.
The issue I am having is the way the code is currently written mainly the GetList method actually returns every record from the database and the filtering is done at the getfull method.
I tryed changing this by passing the id of the adjustment to the getlist method however when i try to do .Where(a=>a.Id == journalid)
i get this error
Cannot implicitly convert type ‘System.Linq.IQueryable to ‘System.Data.Objects.ObjectQuery
public Adjustment GetFull(int id) {
var result = GetList(id).FirstOrDefault(ja => ja.Id == id);
if(result == null) {
throw new InvalidOperationException(
String.Format(" Adjustment with Id={0} not found.", id)
);
}
return LoadFullActivities(result);
}
private IQueryable<Adjustment> GetList(int journalid) {
return GetList(journalid,"Original", "Activities",
"Original.Employee", "Original.Employee.EmployeeType",
"Original.Employee.WorkType", "Original.Employee.Department",
"Original.Activities", "Original.Status",
"Original.Approver", "Status");
}
private IQueryable<Adjustment> GetList(int journalid, params string[] includes) {
var tempResult = dataContext.AdjustmentSet;
foreach (var property in includes) {
tempResult = tempResult.Include(property).Where(a=>a.Id == journalid) ;
}
return tempResult;
}
This would fix the error
Those Includes slow down the query. Since the query is going to get only one row (and do other joins because of the includes) I don’t think the performance is so slow. To increase the performance add indexes for each foreign key. I guess a.Id has already a clustered index.