使用IDbConnection.Query()返回IEnumerable的AsList()是否优于ToList()? [英] Is AsList() better than ToList() with IDbConnection.Query() which returns IEnumerable?
问题描述
我从Marc Gravell(@MarcGravell)阅读了以下答案: https://stackoverflow.com/a/47790712/5779732
I read this answer from Marc Gravell (@MarcGravell): https://stackoverflow.com/a/47790712/5779732
最后一行说:
作为代码的次要优化:最好使用AsList()而不是ToList()以避免创建副本.
As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy.
该语句与QueryMultiple()
有关,该语句返回GridReader
.
That statement is about QueryMultiple()
which returns GridReader
.
据我了解,System.Linq
提供了扩展方法IEnumerable.ToList()
.以下是来自 Microsoft 关于ToList()
.
In my understanding, System.Linq
provides an extension method IEnumerable.ToList()
. Following is from Microsoft about ToList()
.
ToList(IEnumerable)方法强制立即进行查询评估,并返回包含查询结果的List.您可以将此方法附加到查询中,以获得查询结果的缓存副本.
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()
将始终返回IEnumerable
或null
.可以在调用代码中轻松进行空检查.那么AsList
有什么不同?
IDbConnection.Query()
will ALWAYS return IEnumerable
or null
. Null-check could be easily done in calling code. What difference does AsList
makes then?
如果我的理解正确,那么AsList
始终会内部调用ToList
来创建副本.
If my understanding is correct, AsList
will always internally call ToList
which will create a copy.
考虑到这一点,AsList()
是否优于返回IEnumerable
的IDbConnection.Query()
的ToList()
?如是;为什么?
Considering this, is AsList()
better than ToList()
with IDbConnection.Query()
which returns IEnumerable
? If yes; why?
AsList()
在内部执行什么操作使其在这种情况下成为更好的选择?
What is that AsList()
does internally that makes it a better choice in this case?
推荐答案
AsList
是自定义的Dapper扩展方法.它所做的只是检查您传递给它的IEnumerable<T>
是否真的是List<T>
.如果是-则将其返回,仅强制转换为List<T>
.如果不是,则调用常规ToList
.关键是-ToList()
始终创建一个副本,即使传递给它的已经是列表. AsList()
方法避免执行此复制,因此在不需要此复制时非常有用.
AsList
is a custom Dapper extension method. All it does is checks if IEnumerable<T>
you pass to it is really List<T>
. If it is - it returns it back, just casts to List<T>
. If it's not - it calls regular ToList
. The point is - ToList()
always creates a copy, even if what you pass to it is already a list. AsList()
method avoids doing this copy, and so is useful if such copy is unnecessary.
在这种特定情况下,您具有以下代码:
In this specific scenario, you have the following code:
multipleresult.Read<MerchantProduct>()
,其中multipleresult
是GridReader
. Read
具有buffered
参数,默认情况下为true.当它为true时,Read
会真正返回List<T>
,因此通过调用ToList
,您将在没有太多理由的情况下再次复制该列表.
where multipleresult
is GridReader
. Read
has buffered
argument which is true by default. When its true - Read
will really return List<T>
, so by calling ToList
you will copy that list again without much reason.
对于IDbConnection.Query()
来说也是如此-也具有buffered
参数,默认情况下为true,因此默认情况下也会返回List<T>
.
The same is true for IDbConnection.Query()
- is also has buffered
parameter, which is true by default, so it will also by default return List<T>
.
如果您更喜欢使用ToList()
-可以将buffered: false
传递给Query()
或Read()
以避免创建该额外副本.
If you prefer to use ToList()
- you can pass buffered: false
to Query()
or Read()
to avoid creating that additional copy.
这篇关于使用IDbConnection.Query()返回IEnumerable的AsList()是否优于ToList()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!