自动复制列表复制列表(Automapper copy List to List)
我有这些课程:
public class Person { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } public class PersonView { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} }我定义了这个:
Mapper.CreateMap<Person, PersonView>(); Mapper.CreateMap<PersonView, Person>() .ForMember(person => person.Id, opt => opt.Ignore());这是为了工作:
PersonView personView = Mapper.Map<Person, PersonView>(new Person());我想要做同样的,但是List<Person> to List<PersonView>但是我找不到正确的语法。
谢谢
I have these classes :
public class Person { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } public class PersonView { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} }I defined this :
Mapper.CreateMap<Person, PersonView>(); Mapper.CreateMap<PersonView, Person>() .ForMember(person => person.Id, opt => opt.Ignore());That's work for this :
PersonView personView = Mapper.Map<Person, PersonView>(new Person());I'd like make the same but for List<Person> to List<PersonView> but I don't find the right syntax.
Thanks
最满意答案
创建地图(您已经完成,您不需要重复列表),就像以下一样简单:
List<PersonView> personViews = Mapper.Map<List<Person>, List<PersonView>>(people);您可以在AutoMapper文档中了解更多列表和数组 。
Once you've created the map (which you've already done, you don't need to repeat for Lists), it's as easy as:
List<PersonView> personViews = Mapper.Map<List<Person>, List<PersonView>>(people);You can read more in the AutoMapper documentation for Lists and Arrays.
更多推荐
发布评论