Last active
January 31, 2017 16:10
-
-
Save chrispickford/a1dd920fe4aae379847140cdda6d6cd4 to your computer and use it in GitHub Desktop.
SO-41961623
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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