繁体   English   中英

AutoMapper-将两个不同类型的源集合映射到一个目标集合中

[英]AutoMapper - Map two source collections of different types into a single destination collection

我在不同类型的源对象上有两个集合。 我想将两个集合(两个的并集)映射到具有来自两个源类型的所有成员的类型的单个目标集合。 如果我这样做:

CreateMap<Company, CompanyResponse>()
                .ForMember(x => x.Owners, m => m.MapFrom(x => x.BusinessOwners))
                .ForMember(x => x.Owners, m => m.MapFrom(x => x.IndividualOwners));

它仅映射最后一个映射。 我尝试了更精细的映射,但这似乎破坏了自动映射器所做的实体框架投影集成。 我正在使用ProjectTo。

这是我尝试过的方法,它也很好地传达了我想要的结果。

    CreateMap<Company, CompanyResponse>()
        .ForMember(x => x.Owners, m => m.ResolveUsing(x => x.BusinessOwners.Select(o => new OwnerResponse
         {
             Type = UpdateRegistrationCommand.CompanyUpdate.OwnerType.Business,
             Address = o.Address,
             PercentageShareholding = o.Percentage,
             BusinessName = o.Name,
             BusinessNumber = o.Number
         })
        .Union(x.IndividualOwners.Select(o => new OwnerResponse
        {
            Type = UpdateRegistrationCommand.CompanyUpdate.OwnerType.Individual,
            Address = o.Address,
            PercentageShareholding = o.Percentage,
            Title = o.Title,
            FirstName = o.FirstName,
            MiddleNames = o.MiddleNames,
            LastName = o.LastName
        }))));

有人做过这样的事情吗?

根据AutoMapper的文档 ,您可能想尝试ProjectUsing<>()而不是ResolveUsing<>()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM