I want to avoid duplicating the src.Statuses.Where(s => s.StatusType!.StatusGroupId == 1).OrderByDescending(s => s.CreatedUTC).First() from the below mapping. Without changing the shape of my destinationDto.
I'm aware that I could change the DestinationDto to hold a "StatusDto" object which could then have it's own projection defined, and achieve it that way.
Does automapper have some syntax to do this without having to create extra dto's that reflect the source structure?
Basically a way to say
var status = src.Statuses.Where(s => s.StatusType!.StatusGroupId == 1).OrderByDescending(s => s.CreatedUTC).First()
and then use that status across multiple .ForCtorParam
The source looks like this:
Source 1 -- 0..N Status
Status 0..N -- 1 StatusType
StatusType 0..N -- 1 StatusGroup
public class DestinationDto
{
public DestinationDto(...)
public int Id { get; set; }
public DateTime StatusDate { get; set; }
public string Open { get; set; }
public string Status { get; set; }
public DateTime Created { get; set; }
}
public class DestinationProfile : Profile
{
public DestinationProfile()
{
CreateProjection<SourceType, DestinationDto>(MemberList.Destination)
.ForCtorParam(nameof(DestinationDto.Id), opt => opt.MapFrom(src => src.Id))
.ForCtorParam(nameof(DestinationDto.StatusDate),
opt => opt.MapFrom(src => src.Statuses.Where(s => s.StatusType!.StatusGroupId == 1).OrderByDescending(s => s.CreatedUTC).First().CreatedUTC))
.ForCtorParam(nameof(DestinationDto.Open),
opt => opt.MapFrom(src => src.Statuses.Where(s => s.StatusType!.StatusGroupId == 1).OrderByDescending(s => s.CreatedUTC).First().StatusType!.IsOpen))
.ForCtorParam(nameof(DestinationDto.Status),
opt => opt.MapFrom(src => src.Statuses.Where(s => s.StatusType!.StatusGroupId == 1).OrderByDescending(s => s.CreatedUTC).First().StatusType!.Label))
.ForCtorParam(nameof(DestinationDto.Created), opt => opt.MapFrom(src => src.CreatedUTC));
}
}