Automapper map a single object to an array of object

Automapper map object to an array of object

AutoMapper is an object mapper that helps you transform one object of one type into an output object of a different type.

We already learned in our article on Getting started with Automapper in ASP.NET Core.

Today in this article we shall see how to map a single object to an array of objects using Automapper.

We shall also see how to map an array of objects into a single object using an auto mapper.

Today in this article, we will cover below aspects,

automapper mapping from an array to one field

In the above example, we are mapping array fields from Employee and [] Employee fields.

DI IMapper into the module

Dependency Injection of the IMapper interface is explained here already with more details,

Configuring Profile Instances

Here you need to create a class that inherits from the class Profile then please add your configuration of mapping into the Constructor.

Profiles are one of the best way of organizing all your mapping configurations in one place.

Below is Automapper profile for the array of object mapping.

 public class SourceMappingProfile : Profile
    {
        public SourceMappingProfile()
        {
            CreateMap<Source, Destination>()
            .ForMember(dest=>dest.Employee, o => o.MapFrom(src => new[] { src.Employee }))
            .ForMember(dest => dest.Location, o => o.MapFrom(src => src.Address));

        }

Here is a simplified example to perform Mapping object to an array of objects,


        [HttpPost]
        public IActionResult UpdateEmployee([FromBody] Source soureObject)
        {
            var destObject = _mapper.Map<Destination>(soureObject);

            _logger.LogInformation("Mapping successful");

            return Ok(destObject);
        }
automapper map a single object with array propertyAutomapper map object to an array of object

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



8 thoughts on “Automapper map object to an array of object

  1. Any way to do this for a ProjectTo? I am trying to do this in a projection and it can’t create the Linq query.

    1. Hello Jeff- Thanks for your query. Yes, you can do mapping using ProjectTo as well. I will soon put an article on this. Could you elaborate more on the error/issue with Linq ?

      1. I am doing reconciling of Asks to Settlements.
        Object model: 0+ Asks to a Reconcile and 0+ Settlements to a Reconcile.
        The issue I have is when I am trying to Combine into one view Model, call ReconcileViewModel for a unified view.

        E.g. On the Ask=>ReconileViewModel, I map the fields that directly to Reconcile, and I want to put the Ask (source) as the only entry in the Asks list.

        ReconcileViewModel has List Asks
        In Local Mapper, this works:
        CreateMap().ForMember(m => m.Asks, opt => opt.MapFrom(src => src.Reconcile != null ? src.Reconcile.Asks : new List { src }))

        But I can’t seem to get it to work in a projection.

  2. But when I am trying to do the same like

    from public class Availablity : ModifiedDateEntity
    {
    public int Id { get; set; }
    public int PlayerId { get; set; }
    public int EarlyMorning { get; set; } = 0;
    public int Morning { get; set; } = 0;
    public int Evening { get; set; } = 0;
    public Days Day { get; set; }

    }

    into

    public class AvailablityDTO
    {
    public int PlayerId { get; set; }
    public List Availablity { get; set; }
    }

    and Slot will be
    public class Slots
    {
    public int EarlyMorning { get; set; } = 0;
    public int Morning { get; set; } = 0;
    public int Evening { get; set; } = 0;
    public Days Day { get; set; }
    }

    it is not doing giving error of unsupported mappings

Leave a Reply

Your email address will not be published. Required fields are marked *