我想在调用 Map 函数时忽略一些映射。
这是我创建 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())
;
在我的 Controller 中我这样做:
users = Mapper.Map<UserViewModel[]>(response.users);
我想在此调用中忽略这些属性的映射:WebLicenseCount
、MobileLicenseCount
、ExcelAddInLicenseCount
。
有办法做到这一点吗?
最佳答案
如果这是你只想做一次的事情,那么在 Map() 函数之后用相应的 response.users 覆盖 users 中提到的属性。如果这是您想要经常做的事情,那么这意味着您需要更改映射或为这些情况创建第二种类型(例如 SpecialUser)并为此类型定义不同的映射集。
关于c# - AutoMapper:调用Map()函数时忽略一些参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690105/