Frage

I'm trying to page a data set in a WPF application.

My service method looks like this:

public ObservableCollection<MyModel> GetPageOfModels(int projectId, int numSkip, int numResults)
{
  this.db.Set<MyModel>()
    .Where(x => x.Project.Id == projectId)
    .OrderBy(x => x.Name)
    .Skip(numSkip)
    .Take(numResults)
    .Load();

  return this.db.Set<MyModel>().Local;
}

This works great the first time it is hit. But when I move to a different page, Load() is adding the next page to the set, rather than replacing it.

If I recreate the data context before each call to GetPageOfModels it works, but I need to keep the same data context. Is there any way to achieve this?

War es hilfreich?

Lösung

If you must keep the context then you could detach everything before getting the next page.

foreach(var entity in db.Set<MyModel>().Local.ToList())
   db.Entry(entity).State = EntityState.Detached;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top