如何使用AutoMapper从ienumerable(或数组)列表属性映射到列表?

时间:2013-10-15 03:26:44

标签: c# arrays automapper automapper-3

我正在使用AutoMapper来尝试简化某些REST API响应的响应。我有一个名为Messages的数组属性的对象,我想将它映射到列表<>在我的客户端应用程序中的数组

我尝试过各种变体:

Mapper.CreateMap<MessagesResponse, Message[]>()
                      .ForMember(dest => dest, opt => opt.MapFrom(src => src.Messages)); 
....
Mapper.CreateMap<MessagesResponse, List<SimplifiedMessage>>().ForMember(dest => dest.Add(new SimplifiedMessage()), opt => opt.MapFrom(src => src.Messages.GetEnumerator()));

......但显然这些似乎都不起作用。理想情况下,我希望最终得到一个SimplifiedMessage列表,我很确定我错过了正确的语法来迭代Messages []。

public class MessagesResponse
{
    [JsonProperty("meta")]
    public Meta Meta { get; set; }

    [JsonProperty("references")]
    public Reference[] References { get; set; }

    [JsonProperty("messages")]
    public Message[] Messages { get; set; }

    [JsonProperty("threaded_extended")]
    public ThreadedExtended ThreadedExtended { get; set; }
}

public class Message
{
    [JsonProperty("content_excerpt")]
    public string ContentExcerpt { get; set; }

    [JsonProperty("chat_client_sequence")]
    public object ChatClientSequence { get; set; }

    [JsonProperty("replied_to_id")]
    public int? RepliedToId { get; set; }

    [JsonProperty("client_url")]
    public string ClientUrl { get; set; }

    [JsonProperty("language")]
    public string Language { get; set; }

    [JsonProperty("sender_type")]
    public string SenderType { get; set; }

    [JsonProperty("attachments")]
    public Attachment[] Attachments { get; set; }

    [JsonProperty("direct_message")]
    public bool DirectMessage { get; set; }

    [JsonProperty("privacy")]
    public string Privacy { get; set; }

    [JsonProperty("body")]
    public Body2 Body { get; set; }

    [JsonProperty("sender_id")]
    public int SenderId { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("thread_id")]
    public int ThreadId { get; set; }

    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("notified_user_ids")]
    public int[] NotifiedUserIds { get; set; }

    [JsonProperty("created_at")]
    public string CreatedAt { get; set; }

    [JsonProperty("message_type")]
    public string MessageType { get; set; }

    [JsonProperty("system_message")]
    public bool SystemMessage { get; set; }

    [JsonProperty("client_type")]
    public string ClientType { get; set; }

    [JsonProperty("web_url")]
    public string WebUrl { get; set; }

    [JsonProperty("group_id")]
    public int GroupId { get; set; }

    [JsonProperty("network_id")]
    public int NetworkId { get; set; }
}

1 个答案:

答案 0 :(得分:3)

我已经删除了类定义,但以下内容应该有助于实现您想要实现的目标

public class MessagesResponse
{
    public Message[] Messages { get; set; }
}

public class Message
{
    public string Content { get; set; }

    public string SenderId { get; set; }

    public int[] NotifiedUserIds { get; set; }

}
public class SimplifiedMessage
{
    public string Content { get; set; }

    public string SenderId { get; set; }

    public string FromIDs { get; set; }
}
static void Main(string[] args)
    {
        SimplifiedMessage[] simplifiedMessages = null;
        MessagesResponse response = new  MessagesResponse
        {
             Messages = new Message[]
             {
                 new Message
                 {
                      NotifiedUserIds = new[]{1,2,3,},
                      Content = "One"
                 } ,
                 new Message
                 {
                      NotifiedUserIds = new[]{4,5,6},
                      Content = "Two"
                 },
                 new Message
                 {
                     NotifiedUserIds = new[]{7,8,9},
                      Content = "Three"
                 }
             }
        };
        var map = Mapper.CreateMap<Message, SimplifiedMessage>()
            .ForMember(s => s.Content, m => m.MapFrom<string>(msg => msg.Content))
            .ForMember(s => s.FromIDs , m => m.MapFrom<string>(msg => string.Join(",",msg.NotifiedUserIds)));
        simplifiedMessages = Mapper.Map<Message[],SimplifiedMessage[]>(response.Messages);
   }