AutoMapper 显式扩展和子属性

问题描述 投票:0回答:1

我们目前使用 AutoMapper 5.1.1 并使用显式扩展功能。我有一个贷款类,其属性为 LoanTypeId-> int ,其他属性的类型为 Property。在某些情况下,我们希望获取属性中的所有内容,即街道和城市,而某些情况下,我们只想获取街道。这是有可能的吗?因为如果我理解的话,我们必须传递“Property”这样的东西,它会从 Property 中获取所有内容。

 _loanRepository.Value.GetById(request.EntityId)
            .ProjectTo<DataAttributeResponse>(mapperConfiguration, null, new[] { "LoanTypeId", "Property" })

 CreateMap<Model.Property, Property>()                
            .ForMember(d => d.Street, src => src.MapFrom(l => l.Street))
            .ForMember(d => d.City, src => src.MapFrom(l => l.City));

CreateMap<Model.Loan, DataAttributeResponse>()
 .ForMember(d => d.LoanTypeId, src => src.MapFrom(l => l.LoanTypeID))
 .ForMember(d => d.Property, src => src.MapFrom(l => l.Property))
 .ForAllMembers(opt => opt.ExplicitExpansion());  
c# mapping automapper explicit
1个回答
0
投票

在membersToExpand parm中动态传递值,就像

List<string> requiredmembers = new List<string>() { "LoanTypeId" };
if (_list.Contains("Street") && _list.Contains("City"))
{
    requiredmembers.Add("Property.Street");
    requiredmembers.Add("Property.City");
}
else
{
    requiredmembers.Add("Property.Street");
}
_loanRepository.Value.GetById(request.EntityId)
        .ProjectTo<DataAttributeResponse>(mapperConfiguration, 
         null, requiredmembers.ToArray());
© www.soinside.com 2019 - 2024. All rights reserved.