簡體   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