您好我正在使用自动映射器来映射条件实体,该条件实体在不使用After map的情况下无法工作,但在After Map中工作正常,我不会使用它。 我想从Domain.PermanentPlacement映射InvoiceLineCreditHeader,但有条件。
以下是映射代码。
MauritiusMapper.CreateMap<Domain.PermanentPlacement, InvoiceLineCreditHeader>()
.ForMember(desc => desc.ApplicantName, o => o.MapFrom(source => source.Candidate.FullName))
.ForMember(desc => desc.DatePlaced, o => o.MapFrom(source => source.PlacementDate))
.ForMember(desc => desc.ApproveDate, o => o.MapFrom(source => source.LastAuditItem.DateOfAction))
.ForMember(desc => desc.CheckedBy, o => o.MapFrom(source => source.StartCheckedBy))
.ForMember(desc => desc.Job, o => o.MapFrom(source => source.JobTitle));
MauritiusMapper.CreateMap<InvoiceLineCreditSearchResult, InvoiceLineCreditData>()
.ForMember(d => d.InvoiceLineCredits, o => o.MapFrom(s => s.Credits))
.ForMember(d => d.InvoiceLineCreditHeader, o => o.MapFrom(s => s.InvoiceLine))
.ForMember(d => d.CreditReasons, o => o.MapFrom(s => s.CreditReasons))
.ForMember(d => d.CreditStatuses, o => o.MapFrom(s => s.CreditStatuses))
.ForMember(d => d.InvoiceLineCreditHeader, o => o.MapFrom(s => s.InvoiceLine.TransactionLines.OfType<PermanentPlacementTransactionLine>().FirstOrDefault()?.PermanentPlacement ?? s.ReissuePermanentPlacement))
这给出了错误
表达式树lambda可能不包含空传播运算符。
Why can't I use the null propagation operator in lambda expressions?
解释了这一点如果我使用Aftermap,它可以正常工作。
MauritiusMapper.CreateMap<InvoiceLineCreditSearchResult, InvoiceLineCreditData>()
.ForMember(d => d.InvoiceLineCredits, o => o.MapFrom(s => s.Credits))
.ForMember(d => d.InvoiceLineCreditHeader, o => o.MapFrom(s => s.InvoiceLine))
.ForMember(d => d.CreditReasons, o => o.MapFrom(s => s.CreditReasons))
.ForMember(d => d.CreditStatuses, o => o.MapFrom(s => s.CreditStatuses))
//.ForMember(d => d.InvoiceLineCreditHeader, o => o.MapFrom(s => s.InvoiceLine.TransactionLines.OfType<PermanentPlacementTransactionLine>().FirstOrDefault()?.PermanentPlacement ?? s.ReissuePermanentPlacement))
.AfterMap((s, d) =>
{
var placement
= s.InvoiceLine.TransactionLines.OfType<PermanentPlacementTransactionLine>().FirstOrDefault()?.PermanentPlacement ?? s.ReissuePermanentPlacement;
// if (placement == null) return;
d.InvoiceLineCreditHeader.ApplicantId = placement.ApplicantId;
d.InvoiceLineCreditHeader.ApplicantName = placement.Candidate.FullName;
d.InvoiceLineCreditHeader.Job = placement.JobTitle;
d.InvoiceLineCreditHeader.DatePlaced = placement.PlacementDate;
d.InvoiceLineCreditHeader.PlacedBy = placement.PlacedBy;
d.InvoiceLineCreditHeader.ApprovedBy = placement.ApprovedBy;
d.InvoiceLineCreditHeader.ApproveDate = placement.LastAuditItem.DateOfAction;
d.InvoiceLineCreditHeader.CheckedBy = placement.StartCheckedBy;
d.InvoiceLineCreditHeader.StartDate = placement.StartDate;
});
自动映射器中有什么办法可以在不使用AfterMap的情况下实现这一点吗?也有条件,但它也使用linq表达。
有什么想法吗?
答案 0 :(得分:3)
您需要在
中找到?.
运算符的替代方法
s.InvoiceLine.TransactionLines.OfType<PermanentPlacementTransactionLine>()
.FirstOrDefault()?.PermanentPlacement
Select
PermanentPlacement
之前FirstOrDefault
非常简单:
s.InvoiceLine.TransactionLines.OfType<PermanentPlacementTransactionLine>()
.Select(e => e.PermanentPlacement).FirstOrDefault()
答案 1 :(得分:1)
您可以使用所需的逻辑创建单独的方法:
private InvoiceLineCreditHeader GetInvoiceLineCreditHeader(InvoiceLineCreditSearchResult result)
{
// todo: get InvoiceLineCreditHeader conditionally
InvoiceLineCreditHeader header = new InvoiceLineCreditHeader();
return header;
}
并将其称为:
.ForMember(d => d.InvoiceLineCreditHeader, o => o.MapFrom(s => GetInvoiceLineCreditHeader(s)))