繁体   English   中英

如何使用 AutoMapper 到 map 从单个值到集合?

[英]How to use AutoMapper to map from single value to a collection?

我有一个来源 class

public class SourcePerson {
    public Ethnicity Ethnicity { get; set; }
}
public enum Ethnicity {}

我需要 map 到目的地 class

public class DestinationPerson {
    public EthnicityType[] Ethnicities { get; set; }
}
public enum EthnicityType {}

我将如何在 AutoMapper 中配置它? 文档提到了从一个集合到另一个集合的映射,但没有提到从标量到集合的映射(除非我错过了)。

绝对明确:我有一个源 XSD 文件,其中包含:

<xs:element name="Ethnicity" type="Ethnicity" minOccurs="1" maxOccurs="1" />

我有一个目标 XSD 文件,其中包含:

<xs:element name="Ethnicities" maxOccurs="1" minOccurs="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Ethnicity" type="EthnicityType" minOccurs="1" maxOccurs="5" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

我已经使用 xsd.exe 工具从这些 XSD 文件中自动生成 C# 类。 现在,当我反序列化符合源 XSD 的 XML 文件时,我得到了为该源 XSD 生成的类。 我需要将其自动映射到为目标 XSD 生成的类中。

我没有大量使用 AutoMapper 的经验,但我知道我之前已经将结果集映射到集合中,并且我认为我以类似于以下方式进行了操作:

CreateMap<Foo, FooDto>().ReverseMap();
var foos = mapper.Map<IEnumerable<Foo>>(fooDtos);

也就是说,AutoMapper 能够根据我从 Web API 调用中获得的另一个集合为我生成对象集合。

我想如果它不能 go 1 到映射中的许多,您总是可以在映射之前将您的单个种族 object 添加到集合中,可以吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM