Accepted answer
  1. your code doesn't show where you open/close the connection; but the reader here will actually only be open while you are iterating the data. deferred execution, etc. the only bit of your code that does this is the .tolist(), so it'll be fine. in the more general case, yes: the reader will be open for the amount of time you take to iterate it; if you do a .tolist() that will be minimal; if you do a foreach and (for every item) make an external http request and wait 20 seconds, then yes - it will be open for longer.
  2. both have their uses; the non-buffered approach is great for huge results that you want to process as a stream, without ever having to load them into a single in-memory list (or even have all of them in memory at a time); returning a list keeps the connection closed quickly, and makes it easy to avoid accidentally using the connection while it already has an open reader, but is not ideal for large results

if you return an iterator block, the caller can decide what is sane; if you always return a list, they don't have much option. a third way (that we do in dapper) is to make the choice theirs; we have an optional bool parameter which defaults to "return a list", but which the caller can change to indicate "return an iterator block"; basically:

bool buffered = true

in the parameters, and:

var data = queryinternal<t>(...blah...);
return buffered ? data.tolist() : data;

in the implementation. in most cases, returning a list is perfectly reasonable and avoids a lot of problems, hence we make that the default.


this answer ignores flaws in the shown implementation and covers the general idea.

it is a tradeoff - it is impossible to tell whether it is a good idea without knowing the constraints of your system - what is the amount of data you expect to get, the memory consumption you are willing to accept, expected load on the database, etc


how long data reader’s connection will be opened?

the connection will remain open until the reader is dismissed, which means that it would be open until the iteration is over.

when i consider code performance factor only, is this a good idea to use yield return instead of adding record into a list and returning the whole list?

this depends on several factors:

  • if you are not planning to fetch the entire result, yield return will help you save on the amount of data transferred on the network
  • if you are not planning to convert returned data to objects, or if multiple rows are used to create a single object, yield return will help you save on the memory used at the peak usage point of your program
  • if you plan to iterate the enture result set over a short period of time, there will be no performance penalties for using yield return. if the iteration is going to last for a significant amount of time on multiple concurrent threads, the number of open cursors on the rdbms side may become exceeded.

Related Query

More Query from same tag