Mapping Lists using Automapper

asp.net MvcAutomapper

asp.net Mvc Problem Overview


I have the classes:

public class Person{ /* Props here */ }

public class PersonViewModel { /* Props here */ }

Then the list:

List<Person> people = new List<Person>();
List<PersonViewModel> peopleVM = Mapper
                                .MapList<Person, PersonViewModel>(people); //Problem here.

What is the correct way to do this?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Mapper.CreateMap<Person, PersonViewModel>();
peopleVM = Mapper.Map<List<Person>, List<PersonViewModel>>(people);
Mapper.AssertConfigurationIsValid();

From Getting Started:

> How do I use AutoMapper? > > First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members. If you have a source member called "FirstName", this will automatically be mapped to a destination member with the name "FirstName". AutoMapper also supports Flattening, which can get rid of all those pesky null reference exceptions you might encounter along the way. > >Once you have your types, and a reference to AutoMapper, you can create a map for the two types. > > Mapper.CreateMap(); > >The type on the left is the source type, and the type on the right is the destination type. To perform a mapping, use the Map method. > > OrderDto dto = Mapper.Map(order);

Solution 2 - asp.net Mvc

Another Solution

List<Person> people = new List<Person>();
List<PersonViewModel> peopelVM;
peopelVM = people.Select(Mapper.Map<Person, PersonViewModel>);

And in the Automapper config

Mapper.CreateMap<Person, PersonViewModel>();

Solution 3 - asp.net Mvc

If you're using IQueryable lists here (from EF or NH, for example) you can use the AutoMapper.IQueryableExtensions methods, Project() and To().

This is my first time with AutoMapper, but I'm succeeding by creating a map for just the model:

Mapper.CreateMap<Person, PersonViewModel>();
Mapper.AssertConfigurationIsValid();

And then using the IQueryableExtension methods Project() and To():

using AutoMapper.QueryableExtensions;
...

IQueryable<Person> people = new List<Person>().AsQueryable(); //actually from ORM
IQueryable<PersonViewModel> peopleVM = people.Project().To<PersonViewModel>();

Solution 4 - asp.net Mvc

In core 1.1 this extension might work:

public static List<TDestination> MapList<TSource, TDestination>(this IMapper mapper, List<TSource> source)
        {
            return source.Select(x => mapper.Map<TDestination>(x)).ToList();
        }

Solution 5 - asp.net Mvc

Another Solution

mapper.Map<IEnumerable<PersonViewModel>>(people);

Solution 6 - asp.net Mvc

You could create an extension method to do something like this using existing mappings for individual items:

public static class AutoMapperExtensions
{
    public static List<TDestination> MapList<TSource, TDestination>(this IMapper mapper, List<TSource> source)
    {
        return mapper.Map<List<TDestination>>(source);
    }
}

Usage:

List<PersonViewModel> peopleVM = _mapper.MapList<PersonViewModel>(people);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawn McleanView Question on Stackoverflow
Solution 1 - asp.net MvcDerek BeattieView Answer on Stackoverflow
Solution 2 - asp.net MvcRamView Answer on Stackoverflow
Solution 3 - asp.net MvcJerphView Answer on Stackoverflow
Solution 4 - asp.net MvcruudjView Answer on Stackoverflow
Solution 5 - asp.net MvcRangaView Answer on Stackoverflow
Solution 6 - asp.net MvcgarrypView Answer on Stackoverflow