我是AutoMapper的新手,我正试图解决一个问题.
如果我有这样的源类:
public class Membership
{
public int MembershipId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string OrganizationName { get; set; }
public List<Address> Addresses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Address类看起来像这样:
public class Address
{
public int AddressId{ get; set; }
public int RefAddressTypeId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public bool IsPreferredAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的目的地类是:
public class UserInformationModel
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Organization { get; set; }
public string EmailAddress { get; set; }
public PhysicalAddress BillingAddress { get; set; }
public PhysicalAddress ShippingAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
目的地地址类是:
public class PhysicalAddress
{
public AddressType AddressType{get; set;}
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经设置了这样的映射:
Mapper.CreateMap<MinistryMattersIntegration.BusinessObjects.Entities.Cokesbury.Membership, UserInformationModel>()
.ForMember(dest => dest.Organization, opt => opt.MapFrom(src=>src.OrganizationName));
Run Code Online (Sandbox Code Playgroud)
这适用于Member到UserInformationModel,但现在我需要让地址工作.但需要注意的一件重要事情是,目的地是单个帐单地址和单个送货地址,而在原始模型中,所有地址都存储为列表.通过查看RefAddressTypdId和IsPreferredAddress,您可以从列表中找到送货地址和帐单地址的方式.对于特定的RefAddressTypeId,可能只存在一个首选地址.
所以,我的问题是,你如何让AutoMapper进行这种映射?是否有可能,或者我最好只使用常规映射代码?
| 归档时间: |
|
| 查看次数: |
6301 次 |
| 最近记录: |