I've seen examples where you can use AutoMapper to combine data from multiple sources to one source.
My question is how can I accomplish this when I am working with lists of data?
EDIT: In my example the List(Person) will map to a List(PersonCompany), however the mapping for the List(Company) to List(PersonCompany) does not map.
EDIT 2: Changed mapper to update the model, however the company mapping is now the only data being mapped.
Say I have the following classes:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string PhNo { get; set; }
}
public class Company
{
public int PersonID { get; set; }
public int EmpNo { get; set; }
public string Title { get; set; }
}
public class PersonCompany
{
public string Name { get; set; }
public string PhNo { get; set; }
public int EmpNo { get; set; }
public string Title { get; set; }
public int ID { get; set; }
}
Now this simple example works...
// This is a simple example using AutoMapper to map 2 source objects into one destination object.
var personMapConfig = new MapperConfiguration(c =>
c.CreateMap<Person, PersonCompany>()
.ForMember(x => x.Name, y => y.MapFrom(a => a.Name))
.ForMember(x => x.PhNo, y => y.MapFrom(a => a.PhNo))
.ForMember(x => x.ID, y => y.MapFrom(a => a.ID))
);
var companyMapConfig = new MapperConfiguration(c =>
c.CreateMap<Company, PersonCompany>()
.ForMember(d => d.EmpNo, opt => opt.MapFrom(s => s.EmpNo))
.ForMember(d => d.Title, opt => opt.MapFrom(s => s.Title))
);
var person = new Person { ID=1, Name = "PersonName", PhNo = "212-000-0000" };
var company = new Company { PersonID = 1, Title = "Associate Director", EmpNo = 10001 };
var model = personMapConfig.CreateMapper().Map<Person, PersonCompany>(person);
companyMapConfig.CreateMapper().Map<Company, PersonCompany>(company, model);
However when I work with lists of data it doesn't.
var personList = new List<Person> { new Person { ID=1, Name="David Johnson", PhNo="(111) 111-1111"},
new Person { ID=2, Name="Marvin Miller", PhNo="(222) 222-2222"},
new Person { ID=3, Name="Jack Wilson", PhNo="(333) 333-3333"}};
var companyList = new List<Company> { new Company { EmpNo= 1, PersonID = 1, Title="Director"},
new Company { EmpNo= 2, PersonID = 2, Title="Surgeon"},
new Company { EmpNo= 3, PersonID = 3, Title="Sales"}};
var personMapConfig = new MapperConfiguration(c =>
c.CreateMap<Person, PersonCompany>()
.ForMember(x => x.Name, y => y.MapFrom(a => a.Name))
.ForMember(x => x.PhNo, y => y.MapFrom(a => a.PhNo))
.ForMember(x => x.ID, y => y.MapFrom(a => a.ID))
);
var companyMapConfig = new MapperConfiguration(c =>
c.CreateMap<Company, PersonCompany>()
.ForMember(d => d.EmpNo, opt => opt.MapFrom(s => s.EmpNo))
.ForMember(d => d.Title, opt => opt.MapFrom(s => s.Title))
);
var model = personMapConfig.CreateMapper().Map<List<Person>, List<PersonCompany>>(personList);
companyMapConfig.CreateMapper().Map<List<Company>, List<PersonCompany>>(companyList, model);
, List>(companyList)` but you do not assign the result of it to some variable.
– MaKCbIMKo May 24 '16 at 14:17