简体   繁体   中英

How to map Object to List<Object> c# Automapper

Any ideas how to map a single object ot list? I have:

class AdditionalData:
string data1
string data2
string data3

class Person:
AdditionalData additionalData
string UCN

class AdditionalDataDTO:
string data1
string data2
string data3
string data4
string data5

class PersonDTO:
AdditionalDataDTO additionalData[]
string UCN

So how to map AdditionalData to List I want the source to become the first object ot list I know how to map AdditionalData to AdditionalDataDTO

It would be much easier if you would provide a complete code example containing your current state and where it fails. So I had to write the code by myself instead of copying it simply from your question. Nevertheless I made the effort to setup everything on myself and also found the solution. ;-)

As usual by AutoMapper, the missing points are the usage of .ConvertUsing() and the overload that provides a ResolutionContext that allows access the mapper itself for recursive calls:

using AutoMapper;

namespace ConsoleApp
{
    public static class Program
    {
        static void Main(string[] args)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Person, PersonDTO>();
                cfg.CreateMap<AdditionalData, AdditionalDataDTO>();
                cfg.CreateMap<AdditionalData, AdditionalDataDTO[]>()
                    .ConvertUsing((s, _, context) =>
                    {
                        var single = context.Mapper.Map<AdditionalDataDTO>(s);
                        return new[] { single };
                    });
            });

            var mapper = config.CreateMapper();

            var source = new Person
            {
                UCN = "MyName",
                AdditionalData = new AdditionalData
                {
                    Data1 = "First",
                    Data2 = "Second",
                    Data3 = "Third",
                }
            };

            var dest = mapper.Map<PersonDTO>(source);
        }
    }

    public class AdditionalData
    {
        public string Data1 { get; set; }
        public string Data2 { get; set; }
        public string Data3 { get; set; }
    }

    public class Person
    {
        public string UCN { get; set; }
        public AdditionalData AdditionalData { get; set; }
    }

    public class AdditionalDataDTO
    {
        public string Data1 { get; set; }
        public string Data2 { get; set; }
        public string Data3 { get; set; }
        public string Data4 { get; set; }
        public string Data5 { get; set; }
    }

    public class PersonDTO
    {
        public string UCN { get; set; }
        public AdditionalDataDTO[] AdditionalData { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM