Have a requirement to map name (Class A) and phone number (Class B) to Class C which has both Name and PhoneNumber. A person (name) can have more than one phone numbers.
public class A
{
public int ID { get; set; }
public string Name { get; set; }
public virtual IList<B> B { get; set; }
}
public class B
{
public int A_ID { get; set; }
public string PhoneNumber { get; set; }
}
public class C
{
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
Getting A class (which has B) details from the database and it needs to be mapped to Class C.
public class Activity
{
public IList<C> GetContacts(string name)
{
using (ModelEntities ctx = new ModelEntities())
{
Mapper.CreateMap<A, C>();
Mapper.CreateMap<B, C>();
var result =
ctx.A.SingleOrDefault(ss => ss.Name == name);
}
}
}
Can anyone help me to map using Automapper?
Thanks
A person (name) can have more than one phone numbers... Aside from not showing any attempt, you haven't described what should happen in this scenario. ClassCexpects one number per one name.IEnumerable<C>perhaps could, but you can't have a single instance represent multiple instances.