Skip to the content.

The Query APIs allow optional buffering, so it returns the value as IEnumerable<T>. However, the default is “buffered”, which means that most of the time, the result actually is a List<T>. If you call .ToList() on that, you create an additional List<T> with the same contents, which is unnecessary.

To avoid this, Dapper provides an AsList() method, which gives you the existing List<T> if it is one, otherwise it creates one.

Bad:

var orders = conn.Query<Order>(sql, args).ToList();

Good:

var orders = conn.Query<Order>(sql, args).AsList();