Mapping collections using AutoMapper

偶尔善良 提交于 2019-11-30 11:50:12

问题


I'm trying to map an array into an ICollection of type <T>.

Basically I want to be able to do:

Mapper.CreateMap<X[], Y>();

Where Y is Collection<T>

Any ideas?


回答1:


You don't need to setup your mapping for collections, just the element types. So just:

Mapper.CreateMap<X, Y>();
Mapper.Map<X[], Collection<Y>>(objectToMap);

See here for more info: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home




回答2:


Now it looks like you can use:

Mapper.CreateMap<X,Y>(); 
var listOfX = Mapper.Map<List<X>>(someIEnumerableOfY);


来源:https://stackoverflow.com/questions/1623993/mapping-collections-using-automapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!