I read this answer from Marc Gravell (@MarcGravell): https://mcmap.net/q/719678/-dapper-vs-ado-net-with-reflection-which-is-faster
The last line says:
As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy.
That statement is about QueryMultiple()
which returns GridReader
.
In my understanding, System.Linq
provides an extension method IEnumerable.ToList()
. Following is from Microsoft about ToList()
.
The ToList(IEnumerable) method forces immediate query evaluation and returns a List that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.
IDbConnection.Query()
will ALWAYS return IEnumerable
or null
. Null-check could be easily done in calling code. What difference does AsList
makes then?
If my understanding is correct, AsList
will always internally call ToList
which will create a copy.
Considering this, is AsList()
better than ToList()
with IDbConnection.Query()
which returns IEnumerable
? If yes; why?
What is that AsList()
does internally that makes it a better choice in this case?
List
data structure, then that list (i.e. instance) will be returned, rather than creating a new (i.e. copy)List
. – LituusIDbConnection.Query()
will ALWAYS returnIEnumerable
ornull
. Null check could be easily done in calling code. What difference doesAsList
makes then? – Unfeeling