score:2

Accepted answer

It turns out that AutoMapper sometimes automatically adds ToList / ToArray to the projection expression when mapping enumerable types, sometimes doesn't.

The rule seems to be as follows. If the destination enumerable type is directly assignable from the source expression type, AutoMapper uses directly the source expression. In other words, if the following assignment is valid (pseudo code):

dst.Member = src.Expression;

In this case, it's up to you to include ToList or not in your mapping expression (thus opt-in for EF Core correlated query optimization).

In all other cases AutoMapper performs enumerable element mapping if needed and then adds either ToArray or ToList. There is no way to opt-out.

Shortly, if the destination enumerable element type if Dto (requires mapping), don't include ToList in source LINQ expression, if it is primitive or entity type, do include ToList to avoid N + 1 queries. All this applies if the destination collection type is IEnumerable<T>. Any other derived collection type like IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IList<T>, List<T>, T[] etc. will be handled automatically by AutoMapper in case the source expression returns IEnumerable<TSource>.


Related Articles