AutoMapper:调用Map()函数时忽略一些参数

时间:2013-10-30 17:40:49

标签: c# automapper

我想在调用Map函数时忽略一些映射。

这是我创建地图的地方:

Mapper.CreateMap<User, UserViewModel>()
    .ForMember(dest => dest.WebLicenseCount, opt => opt.MapFrom(src => GetCount(src, "0801")))
    .ForMember(dest => dest.MobileLicenseCount, opt => opt.MapFrom(src => GetCount(src, "0901")))
    .ForMember(dest => dest.ExcelAddInLicenseCount, opt => opt.MapFrom(src => GetCount(src, "0895")))
    .ForMember(dest => dest.NextExpirationDate, opt => opt.MapFrom(src => src.Licences.Min(l => l.LicenceProducts.Min(lp => lp.ExpirationDate))))
    .ForMember(dest => dest.Licences, opt => opt.MapFrom(src => src.Licences.ToList()))
    .ForMember(dest => dest.TicketsCount, opt => opt.MapFrom(src => src.Tickets.Count(t => t.Status != (int)TicketStatusType.Closed)))
    .ForMember(dest => dest.ParentContact, opt => opt.MapFrom(src => src.Contact))
    .ForMember(dest => dest.ParentUser, opt => opt.MapFrom(src => src.User2))
    .ForMember(dest => dest.Contacts, opt => opt.MapFrom(src => src.Contacts))
    .ForMember(dest => dest.MainContact, opt => opt.Ignore())
    .ForMember(dest => dest.SessionID, opt => opt.Ignore())
    ;

在我的控制器中,我这样做:

users = Mapper.Map<UserViewModel[]>(response.users);

我想在此调用中忽略这些礼节的映射:WebLicenseCountMobileLicenseCountExcelAddInLicenseCount

无论如何都要这样做?

1 个答案:

答案 0 :(得分:-2)

如果您只想做一次,那么在Map()函数之后用相应的response.users覆盖用户中提到的属性。如果这是您经常要做的事情,那么这意味着您需要更改映射或为这些情况创建第二种类型(例如SpecialUser)并为此类型定义不同的映射集。