Automapper - different mapping techniques for sub objects depending on IQueryable.ProjectTo or IEnumerable Map

31 Views Asked by At

If you have an object that has a collection on it as an example of objects, the behavior is obvious to just map it from the collection and the query in EF Core or similar will do it automatically if it's an IQueryable.

The issue comes if it's doing a mapper.Map<> on an already materialized object. Then it has to go and load it from the database using the Data Context.

How does one have AutoMapper use different strategies for the IQueryable versus IEnumerable case?

1

There are 1 best solutions below

1
Gabriel Ribeiro Rossi On

The AutoMapper can handle collections in different ways depending on whether the collection is an IQueryable or an IEnumerable.

For IQueryable, the AutoMapper can apply the mapping configurations directly to the query before it is executed in the database.

For IEnumerable, in this case, the AutoMapper iterates through each item in the collection, applying the object-to-object mapping rules.