Можно ли привязать объект к списку в Automapper? - PullRequest
3 голосов
/ 02 августа 2010

У меня есть класс Foos:

public class Foos
{
    public string TypeName;

    public IEnumerable<int> IDs;
}

Можно ли сопоставить его с AutoMapper объектам IList of Foo?

public class Foo
{
    public string TypeName;

    public int ID;
}

1 Ответ

4 голосов
/ 04 августа 2010

Ответ Ому дал мне идею, как решить проблему (+1 за предложение).Я использовал метод ConstructUsing (), и он работал для меня:

    private class MyProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<Foos, Foo>()
                .ForMember(dest => dest.ID, opt => opt.Ignore());
            CreateMap<Foos, IList<Foo>>()
                .ConstructUsing(x => x.IDs.Select(y => CreateFoo(x, y)).ToList());                
        }

        private Foo CreateFoo(Foos foos, int id)
        {
            var foo = Mapper.Map<Foos, Foo>(foos);
            foo.ID = id;
            return foo;
        }
    }
...