entity framework - Count before order, skip and take -
i'm using entity framework unit-of-work , repository pattern.
for function ordering, pagination, etc. utilize next code:
stammdatenentitymodels = _unitofwork.stammdatenrepository.get() .where( s => s.geloescht == false && ((s.auftraggeber != null && s.auftraggeber.bezeichnung.tolower().contains(keyword)) || (s.seriennummer.contains(keyword)) || (s.bezeichnung.tolower().contains(keyword)) || (s.stammdatenkunde != null && s.stammdatenkunde.name.tolower().contains(keyword)) || (s.beginnvos.hasvalue && s.beginnvos == datetime) || (s.vosdauer != null && s.vosdauer.bezeichnung.tolower().contains(keyword)) || (s.geraetewert.hasvalue && s.geraetewert.value.tostring().contains(keyword)) )) .orderby(orderby) .skip(inputmodel.entriestodisplay*(inputmodel.page - 1)) .take(inputmodel.entriestodisplay) .tolist();
now need know numbers of records, before skip , take (for pagination) performed.
therefore have same code again:
totalcount = _unitofwork.stammdatenrepository.get() .count( s => s.geloescht == false && ((s.auftraggeber != null && s.auftraggeber.bezeichnung.tolower().contains(keyword)) || (s.seriennummer.contains(keyword)) || (s.bezeichnung.tolower().contains(keyword)) || (s.stammdatenkunde != null && s.stammdatenkunde.name.tolower().contains(keyword)) || (s.beginnvos.hasvalue && s.beginnvos == datetime) || (s.vosdauer != null && s.vosdauer.bezeichnung.tolower().contains(keyword)) || (s.geraetewert.hasvalue && s.geraetewert.value.tostring().contains(keyword)) ));
unfortunately leads lot of redundance , query performed twice. there improve solution?
i agree redundancy. can cache count result specific parameters (keyword, datetime) amount of time , utilize 1 time again @ subsequent calls deliver paginated (skip, take) results stammdatenrepository.
this way have overall count-call 1 time specific parameters.
found this question respected fellow member states:
thinking sql point of view, can't think of way in single normal query retrieve both total count , subset of data, don't think able in linq either.
so, think have cache counts results increment performance. know best how specific situation , if it's worth @ all...
entity-framework entity-framework-6
No comments:
Post a Comment