AutoMapper: 如何排除值类型映射并处理空值
您可以使用以下方式来排除值类型(即结构体)的映射:
CreateMap<Source, Destination>()
.ForMember(dest => dest.ReferenceProperty, opt => opt.Condition(src => src.ReferenceProperty != null))
.ForMember(dest => dest.ValueProperty, opt => opt.Ignore());
在上面的示例中,假设ValueProperty是一个值类型的属性,您可以使用.Ignore()方法来排除它的映射。这样,当ValueProperty的值为null时,它将被映射为默认值。请注意,值类型的默认值通常是0或false。
原文地址: https://www.cveoy.top/t/topic/qlDa 著作权归作者所有。请勿转载和采集!