public class Car
{
public int Id {get;set;}
public string Name {get;set;}
public Owner OwnerData {get;set}
}
public class Owner
{
public int Id {get;set;}
public string Name {get;set;}
public string Phone {get;set;}
public string CarName {get;set;}
}
Owner ownerData = repository.Get<Owner>(id);
IEnumerable<Car> cars = repository.Get<Car>();
// map all objects in the collection cars and set OwnerData
var data = new List<Car>();
foreach(var car in cars) //replace this with automapper
{
car.OwnerData = new Owner
{
CarName = ownerData.CarName,
Name = ownerData.Name,
Phone = ownerData.Phone
};
data.Add(car);
}
How can I take advantage of automapper and replace whole foreach with automapper (iterate through whole cars collection and set Owner data?