Automapper跳过所有类的空值 [英] Automapper skip null values for all classes

查看:114
本文介绍了Automapper跳过所有类的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此链接中,我们可以将Automapper设置为跳过源对象中的空值.我们可以自动为所有类别配置条件吗?谢谢.

In this link we can set Automapper to skip null value from source object. Can we configure the condition for all classes automatically? Thanks.

推荐答案

您可以执行以下操作:

            Mapper.Initialize(cfg =>
            {
                cfg.ForAllMaps((typeMap, map) =>
                    map.ForAllOtherMembers(opt => opt.Condition((src, dest, srcMem, destMem) => src != null)));
}

我使用了 ForAllOtherMembers ,这样它不会覆盖您可能单独声明的其他条件.

I used ForAllOtherMembers so that it does not override your other conditions that you may declare individually.

这篇关于Automapper跳过所有类的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆