开发者

Automapper ConfigurationExpression Condition

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜