开发者

Automapper ConfigurationExpression Condition

开发者 https://www.devze.com 2023-02-04 04:15 出处:网络
Quick question - whats the difference in the following? This one works: CreateMap<OrderResult, OrderViewModel>()

Quick question - whats the difference in the following?

This one works:

CreateMap<OrderResult, OrderViewModel>()
    .ForMember(x => x.SoldTo, opt => opt.Ig开发者_StackOverflownore())
    .ForMember(x => x.ShipTo, opt => opt.Ignore())
    .ForMember(x => x.ShowPlaceOrder, opt => opt.MapFrom(c => c.Messages.Count == 0));

I would expect this one to do pretty much the same thing except "Mapper.AssertConfigurationIsValid();" fails on this one saying that "ShowPlaceOrder" is not mapped.

CreateMap<OrderResult, OrderViewModel>()
    .ForMember(x => x.SoldTo, opt => opt.Ignore())
    .ForMember(x => x.ShipTo, opt => opt.Ignore())
    .ForMember(x => x.ShowPlaceOrder, opt => opt.Condition(c => c.Messages.Count == 0));

Thanks Joe


You still need to provide a source for ShowPlaceOrder in case the condition is true. The "Condition" method takes a predicate to decide if the mapping should be done. I think your first example is more clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号