Mapping multiple collections to a single collection using Automapper

1.2k views Asked by At

I am able to map group of sametype of collections to a single collection using the below code.

AutoMapper.Mapper.CreateMap<Source, Destination>().ForMember(
                dest => dest.Drivers,
                opt => opt.MapFrom(src => src.BikeDrivers.Concat(src.CarDrivers).Concat(src.TruckDrivers))); 

With the above solution I am able to map all the three type of drivers into one collection. My destination object (Driver) has a property called DriverType which helps in identifying the type of driver. (BikeDriver/CarDriver/TruckDriver)

In the above code, how i can set the DriverType property based on the collection I am adding.

for eg: i have to hard code

DriverType = CarDriver for CarDrivers collection items DriverType = BikeDriverfor BikeDrivers collection item.

Thanks in advance

1

There are 1 answers

0
Andriy Zakharko On

To set the DriverType property you have to have this knowledge in your source object. I can't see your big picture, but this maybe used as a sample

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = new Source()
                        {
                            BikeDrivers = new List<BikeDriver>() {new BikeDriver()},
                            CarDrivers = new List<CarDriver>() {new CarDriver()},
                            TruckDrivers = new List<TruckDriver>() {new TruckDriver()},
                        };

            var d = new Destination();


            AutoMapper.Mapper.CreateMap<Source, Destination>().ForMember(
                dest => dest.Drivers,
                opt => opt.MapFrom(src => src.BikeDrivers.Concat<IDriver>(src.CarDrivers).Concat<IDriver>(src.TruckDrivers)));

            var result = AutoMapper.Mapper.Map(s, d);
        }

        public class Driver : IDriver
        {
            public string DriverType { get; set; }
        }

        public class Destination
        {
            public IEnumerable<IDriver> Drivers { get; set; }
        }

        public class Source
        {
            public IEnumerable<BikeDriver> BikeDrivers { get; set; }
            public IEnumerable<CarDriver> CarDrivers { get; set; }
            public IEnumerable<TruckDriver> TruckDrivers { get; set; }
        }

        public interface IDriver
        {
            string DriverType { get; set; }
        }

        public class BikeDriver : IDriver
        {
            public string DriverType
            {
                get { return "BikeDriver"; }
                set { throw new NotImplementedException(); }
            }
        }
        public class CarDriver : IDriver
        {
            public string DriverType
            {
                get { return "CarDriver"; }
                set { throw new NotImplementedException(); }
            }
        }
        public class TruckDriver : IDriver
        {
            public string DriverType
            {
                get { return "TruckDriver"; }
                set { throw new NotImplementedException(); }
            }
        }
    }
}