Mapping Lists using Automapper

后端 未结 5 1630
挽巷
挽巷 2020-12-02 17:52

I have the classes:

public class Person{ /* Props here */ }

public class PersonViewModel { /* Props here */ }

Then the list:



        
5条回答
  •  温柔的废话
    2020-12-02 18:30

    You could create an extension method to do something like this using existing mappings for individual items:

    public static class AutoMapperExtensions
    {
        public static List MapList(this IMapper mapper, List source)
        {
            return mapper.Map>(source);
        }
    }
    

    Usage:

    List peopleVM = _mapper.MapList(people);
    

提交回复
热议问题