using System.Collections.Generic;
public static void Main()
var flattenData = new List<CountyAndCity>
new CountyAndCity { CountyId = 1, CountyName = "Albany", CityId = 10, CityName = "Cohoes", CityPopulation = 1000 },
new CountyAndCity { CountyId = 1, CountyName = "Albany", CityId = 20, CityName = "Watervliet", CityPopulation = 1000 },
new CountyAndCity { CountyId = 2, CountyName = "Bronx", CityId = 30, CityName = "Wakefield", CityPopulation = 2000 },
new CountyAndCity { CountyId = 2, CountyName = "Bronx", CityId = 40, CityName = "Eastchester", CityPopulation = 2000 },
var unflattenData = new List<County>
new County { Id = 1, Name = "Albany", Cities = new List<City>
new City { Id = 10, Name = "Cohoes", Population = 1000 },
new City { Id = 20, Name = "Watervliet", Population = 1000 },
new County { Id = 2, Name = "Bronx", Cities = new List<City>
new City { Id = 30, Name = "Wakefield", Population = 2000 },
new City { Id = 40, Name = "Eastchester", Population = 2000 },
var mapperConfiguration = new MapperConfiguration(configuration => configuration.AddProfile(new AutoMapperProfile()));
var mapper = mapperConfiguration.CreateMapper();
State mappedData = mapper.Map<State>(flattenData);
bool result = Enumerable.SequenceEqual(mappedData.Counties, unflattenData);
Console.WriteLine(result);
Console.WriteLine(JsonConvert.SerializeObject(mappedData, Formatting.Indented));
public class CountyAndCity
public int CountyId { get; set; }
public string CountyName { get; set; }
public int CityId { get; set; }
public string CityName { get; set; }
public int CityPopulation { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public int Population { get; set; }
public override bool Equals (Object obj)
&& Population == b.Population;
public override int GetHashCode()
return this.Id.GetHashCode() ^ this.Name.GetHashCode() ^ this.Population.GetHashCode();
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<City> Cities { get; set; }
public override bool Equals (Object obj)
&& Enumerable.SequenceEqual(Cities, b.Cities);
public override int GetHashCode()
return this.Id.GetHashCode() ^ this.Name.GetHashCode();
public List<County> Counties { get; set; }
public class AutoMapperProfile : Profile
public AutoMapperProfile()
CreateMap<CountyAndCity, City>()
.ForMember(destination => destination.Id, options => options.MapFrom(source => source.CityId))
.ForMember(destination => destination.Name, options => options.MapFrom(source => source.CityName))
.ForMember(destination => destination.Population, options => options.MapFrom(source => source.CityPopulation));
CreateMap<List<CountyAndCity>, State>()
.ForMember(destination => destination.Counties, options => options.MapFrom<StateCountiesResolver>());
public class StateCountiesResolver : IValueResolver<List<CountyAndCity>, State, List<County>>
public List<County> Resolve(List<CountyAndCity> src, State dest, List<County> destMember, ResolutionContext ctx)
destMember = src.GroupBy(x => x.CountyId)
Name = x.FirstOrDefault()?.CountyName,
Cities = ctx.Mapper.Map<List<City>>(x.ToList())