C# 使用 AutoMapper 映射集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1623993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:29:08  来源:igfitidea点击:

Mapping collections using AutoMapper

c#automapper

提问by Brian Liang

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

我正在尝试将数组映射到ICollection类型<T>.

Basically I want to be able to do:

基本上我希望能够做到:

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

Where Yis Collection<T>

哪里YCollection<T>

Any ideas?

有任何想法吗?

采纳答案by Drew Freyling

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

有关更多信息,请参见此处:http: //automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home

回答by tmgirvin

Now it looks like you can use:

现在看起来你可以使用:

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