0

I'm using a IEnumerable which I have saved in the cache. I need a copy of this list and transform the contents. This copy is temporary and is not saved.

Currently I am using IClonable on MyObject to create a new list. This works but causes EF to take a second trip to the database, thus its very slow. How can I clone this list without EF going to the database? The data is already in memory in the cache.

Alternatively; is there a better way to copy the list from cache so I can temporarily use it?

Using EF5, .Net 4.5.

This is what I use to clone. Note that residualList is in the cache. But this line still causes EF to get everything from the database.

var clonedList = residualList.Select(x => (Product) x.Clone()).ToList();

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Christiaan Maks
  • 3,248
  • 3
  • 23
  • 27
  • 1
    several solutions: detach the list from the context or use the Local property of the DbSet, or serialize the List and unserialize for duplication. BTW this sounds like residualList is a IQueryable or a DbQuery, it should not in your use case. – tschmit007 Jan 13 '15 at 16:22
  • residualList is a List (created from dbContext with the .ToList() method so should be in memory). Ill try your suggestions in the morning, thanks. – Christiaan Maks Jan 13 '15 at 16:30
  • in this case may be tle Clone method fires some virtual properties and you are facing a [n+1 select issue](http://stackoverflow.com/questions/97197/what-is-the-n1-selects-issue) – tschmit007 Jan 13 '15 at 16:38

0 Answers0