I have two entities: Employee
and Team
.
What I want is an EmployeeForm
that has the Name
of the Team
.
How can I achieve this using AutoMapper?
My current "solution" is the following:
Mapper.CreateMap<Employee, EmployeeForm>()
.ForMember(dest => dest.TeamName, opt => opt.MapFrom(x => x.GetTeams().FirstOrDefault() != null ? string.Join(", ", x.GetTeams().Select(y=>y.Name)) : "n/a"));
In my opinion this is bad readable.
What I would like to have is a generic method where I can pass an entity, choosing the collection and saying if collection is null return a default value or otherwise choose the property of the collection via lambda expressions.