Skip to content

Converting collections with MapToList()

The MapToList<TSource, TTarget>() method allows you to transform a collection of source objects into a new list of destination instances.
It follows the same logic as MapTo<T>(), automatically applying conversions based on matching property names and compatible types.


When to use

This method is ideal when converting DTO lists from APIs, commands, events, or any intermediate layer into domain entities.


Practical example

public class UserDto
{
    public string Name { get; set; }
    public string Role { get; set; } // will be converted to enum
}

public enum UserRole
{
    Administrator,
    Operator
}

public class User
{
    public string Name { get; set; }
    public UserRole Role { get; set; }
}

Converting a list of UserDto to User:

var dtoList = new List<UserDto>
{
    new UserDto { Name = "Anna", Role = "Administrator" },
    new UserDto { Name = "John", Role = "Operator" }
};

var users = dtoList.MapToList<UserDto, User>();

Each item in the list is converted using the same mapping rules as MapTo<T>().


Benefits

  • Simple and direct batch conversion
  • Reuses the same compiled expression cache used by MapTo<T>()
  • No need for manual foreach loops or external libraries

Considerations

  • The source list cannot be null (throws exception)
  • The target type must have a public parameterless constructor
  • Conversions follow the same rules as MapTo<T>()

Fast and predictable

MapToList<TSource, TTarget>() is ideal for data pipelines, bulk transformations, and API input layers.