【发布时间】:2021-01-27 04:43:03
【问题描述】:
我在 C# 中使用 Automapper。
目前我正在尝试使用以下代码进行映射。
CreateMap<Student, StudentDto>()
.ForMember(dest => dest.FeeType, opt =>
{
opt.PreCondition(src => (src.Key == "Paid"));
opt.MapFrom(src => src.Value);
});
我只想在密钥 == 付费时进行映射,否则它不应该映射。但是在这种情况下,它只是传递 null。
假设集合有 100 条记录,并且只有 1 条符合条件,其他 99 条记录作为 NULL 传递。
编辑 -- 我正在使用 Azure Function Apps,我的 Automapper 版本是 10.0.0
请指教。
【问题讨论】:
-
答案是 Automapper 6,我用的是 10。我试过 ForAllMaps((obj, cnfg) => cnfg.ForAllMembers(opts => opts.Condition((src, dest, srcMember) = > srcMember != null)));但它没有用。
标签: c# .net-core automapper