Skip to content

Instantly share code, notes, and snippets.

@chrispickford
Last active January 31, 2017 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrispickford/a1dd920fe4aae379847140cdda6d6cd4 to your computer and use it in GitHub Desktop.
Save chrispickford/a1dd920fe4aae379847140cdda6d6cd4 to your computer and use it in GitHub Desktop.
SO-41961623
using System.Collections.Generic;
using AutoMapper;
namespace ConsoleApplication2
{
internal class Program
{
private static void Main(string[] args)
{
InitAutoMapper();
var parent = new Parent
{
Locations = new List<Location>
{
new Location
{
LocationId = 123,
Name = "test"
}
}
};
var destination = Mapper.Map<Destination>(parent);
}
private static void InitAutoMapper()
{
Mapper.Initialize(
cfg =>
{
cfg.CreateMap<Location, int>().ConvertUsing(source => source.LocationId);
cfg.CreateMap<Parent, Destination>()
.ForMember(dest => dest.Locations, opts => opts.MapFrom(src => src.Locations));
});
}
}
public class Parent
{
public List<Location> Locations { get; set; }
}
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
}
public class Destination
{
public List<int> Locations { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment