Search code examples
c#arraysautomapperautomapper-3

How to map from ienumerable (or array) list property to list with AutoMapper?


I'm using AutoMapper in an attempt to simplify the responses from some REST API responses. I have an object with an array property called Messages, and I want to map it to a list<> or array in my client application.

I've tried variations on:

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()));

...but obviously none of these seems to work. Ideally I want to end up with a list of SimplifiedMessage, and I'm pretty sure I'm missing the right syntax to iterate on the 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; }
}

Solution

  • I've stripped down the class definitions but the following should help achieve what you are trying to accomplish

    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);
       }