ersu
ersu

Reputation: 105

C# AutoMapper does not copy Collections

What's the mistake in the following code, when I try to copy on List to an other? The count of list is always 0.

using (WeldingEntities db = new WeldingEntities())
{
    var query = db.Users.Select(x => x).ToList();
        var config = new MapperConfiguration(cfg => cfg.CreateMap<List<User>, List<SimpleUser>>());
        var mapper = config.CreateMapper();
        var list = mapper.Map<List<SimpleUser>>(query);
        return list;
}

Upvotes: 0

Views: 360

Answers (1)

Chris
Chris

Reputation: 4471

You don't specify the collection type in your map definition. You just need to specify the individual type and automapper can figure out collection mapping on its own.

var config = new MapperConfiguration(cfg => cfg.CreateMap<User, SimpleUser>());

See docs for more info on the collection types that are supported.

Upvotes: 2

Related Questions