using System.Collections.Generic;
namespace AutomapperSample
public static class BlogPostDtoExtension
public static TResult MergeInto<TResult>(this IMapper mapper, object destination, object source)
return mapper.Map(source, mapper.Map<TResult>(destination));
public class BlogPostProfile : Profile
CreateMap<BlogPost, BlogPostResponse>();
CreateMap<IList<Tag>, BlogPostResponse>()
.ForMember(d => d.TagNames,
a => a.MapFrom(s => string.Join(',', s.Select(x => x.Name))));
static void Main(string[] args)
var blogPost = new BlogPost
var mappingConfig = new MapperConfiguration(mc =>
mc.AddProfile(new BlogPostProfile());
IMapper mapper = mappingConfig.CreateMapper();
var dto = mapper.MergeInto<BlogPostResponse>(tags, blogPost);
Console.WriteLine(dto.TagNames);
public class BlogPostResponse
public int Id { get; set; }
public string Name { get; set; }
public string TagNames { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public IList<int> TagIds { get; set; }
public int Id { get; set; }
public string Name { get; set; }