自动复制列表复制列表(Automapper copy List to List)

编程入门 行业动态 更新时间:2024-10-27 23:30:35
自动复制列表复制列表(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.

更多推荐

本文发布于:2023-04-28 15:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Automapper   List   copy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!