0

is there a way to get a LINQ query result on a EF DBContext as a 'Typed Data Reader', in sucha a way that when i am reading de IQueryable result (ex with a .ToList()) I don't put all the result in memory?

I am expecting something like this (or equivalent):

var personQueryResult=dbContext.People.Where(…).Select(…).AsDataReader();
foreach(person in personQueryResult){
   //Here I expect that person is typed of the People Dbset<T> type, ex. a Person type and i can do:

person.Name="...";
person.Surname="...";
//etc.
}
2
  • 2
    DataReader - no. But pulling the whole result set in memory via ToList is not mandatory - treating the query as IEnumerable<T> (eventually combined with AsNoTracking) and foreach-in it is pretty much an equivalent of "entity" DataReader because you read one item at the time.
    – Ivan Stoev
    Aug 13, 2018 at 21:00
  • 1
    Oh right! Foreach on IEnumerable<T> is definitely the right approch! Thank you! Aug 16, 2018 at 15:30

1 Answer 1

1

There is not an explicit method that ensure you that that the collection would be scrolled trough a DbDataReader.

Knowing the inheritance hierarchy of the EF's base classes, every usage of a foreach on a DbSet<T>,IQueryable<T> or IEnumerable<T>, avoiding any direct method that will trigger a query deferred execution (ex. ToList()) will prevent to load all the records in memory so that at every "MoveNext()" of the enumerator will be used the "Read()" of the underlying DbDataReader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.