Reputation: 635
I have an EF entity that looks like this:
public class Customer
{
public int IdCustomer {get; private set;}
public string Name {get; private set;}
ICollection<Specialty> Specialties {get;private set;}
}
public class Specialty
{
public int IdSpecialty {get;private set}
public int? IdSpecTypeOne {get;private set;}
public int? IdSpecTypeTwo {get;private set;}
public string Name {get;private set;}
public virtual SpecTypeOne SpecialtyTypeOne {get;private set;}
public virtual SpecTypeTwo SpecialtyTypeOne {get;private set;}
}
public class SpecTypeOne
{
public int IdSpecTypeOne {get;private set;}
public string Name {get;private set;}
}
public class SpecTypeTwo
{
public int IdSpecTypeTwo {get;private set;}
public string Name {get;private set;}
}
and a ViewModel:
public class CustomerViewModel
{
public int IdCustomer
public List<CustomerSpecialtyViewModel> SpecialtyOfTypeOne {get; set;}
public List<CustomerSpecialtyViewModel> SpecialtyOfTypeTwo {get;set;}
}
public class CustomerSpecialtyViewModel
{
public int IdSpecialty {get;set;}
public int IdSpecTypeOne {get;set;}
public int IdSpecTypeTwo {get;set;}
public string SpecTypeName {get;set;}
}
Basically, I want to build my ViewModel like above, flattening Specialty and SpecType to a single ViewModel (CustomerSpecialtyViewModel).
I'm mapping the ViewModel as:
Mapper.CreateMap<Specialty, CustomerSpecialtyViewModel>();
Mapper.CreateMap<Customer, CustomerViewModel>()
.ForMember(d => d.SpecialtyOfTypeOne, opt => opt.MapFrom(s => s.Specialties.Where(p => p.IdSpecType == SpecTypes.TypeOne)))
.ForMember(d => d.SpecialtyOfTypeTwo, opt => opt.MapFrom(s => s.Specialties.Where(p => p.IdSpecType == SpecTypes.TypeTwo)))
How I can include the sub-property SpecType.Name in CustomerSpecialtyViewModel, since this is a sub-property?
CustomerSpecialtyViewModel
{
int IdSpecialty => OK
int IdSpecType => OK
string SpecTypeName => Getting 'NULL' - Need this
}
UPDATE: I have TWO types of SpecType (One and Two), but both have a common property (Name). I only need the property. I´ll check what type is (One or Two) internally in my Application Service. In the context of View, this doesn´t matter.
Upvotes: 0
Views: 933
Reputation: 14697
I think this should work:
Mapper.CreateMap<Specialty, CustomerSpecialtyViewModel>()
.ForMember(d => d.SpecTypeName, o => o.MapFrom(s => s.SpecialtyType.Name));
Upvotes: 1