What I miss in most of the tutorials for automapper is working with lists, so with that basic mapping, we are able to map a list of users to a list of usersDTO as well.
mapper.Map<List<UserDTO>>(users);// where variable users has List<User> type
But in your example where the list is a property on another mapped object you might need one other thing. If that property could be null, you’ll want to add something to your configuration:
The AllowNullCollections will prevent null exception errors on collections. By default, AutoMapper behaves like Entity Framework in that it believes collection properties should never be null.
What I miss in most of the tutorials for automapper is working with lists, so with that basic mapping, we are able to map a list of users to a list of usersDTO as well.
Oh yeah! All the basic collection types are supported so you can do anything like:
But in your example where the list is a property on another mapped object you might need one other thing. If that property could be null, you’ll want to add something to your configuration:
The
AllowNullCollections
will prevent null exception errors on collections. By default, AutoMapper behaves like Entity Framework in that it believes collection properties should never be null.I like to always initialize my collections, but thanks for this information.