c# - 覆盖 CreateMap - Automapper
问题描述
我有以下地图。所有十进制类型数字都有 2 个小数位。
CreateMap<decimal, decimal>().ConvertUsing(x => decimal.Round(x, 2, MidpointRounding.AwayFromZero));
但有时,我需要更多小数位,例如
CreateMap<T, R>.ForMember(x => x.Total, x => x.MapFrom(x => decimal.Round(x.Items.Sum(x => x.Price * x.Quantity), 3, MidpointRounding.AwayFromZero)))
是否可以在某些“CreateMap”中覆盖全局 CreateMap?谢谢
解决方案
One option is that when you call the mapper.Map, you can provide options that override the current map profile using the inline mapping like so.
_mapper.Map(T, R, opt => opt.ConfigureMap().ForMember(x => x.Total, x => x.MapFrom(x => decimal.Round(x.Items.Sum(x => x.Price * x.Quantity), 3, MidpointRounding.AwayFromZero)));
推荐阅读
- drools - 在工作台中如何检索业务流程中任务中的工作项结果?
- c++ - 确定 std::shared_ptr 是否是对对象的唯一引用
- cpu-usage - 使用 slurm 显示使用的 CPU 小时数
- simulation - 如何使用 CCA 解决基于发送方的乐团调度中的 RX 问题?
- vb.net - “Sub mdichild_Shown()”事件未触发
- python - How to efficiently search for a list of strings in another list of strings using Python?
- python - 使用 python Pandas 进行数据重排 | 基于重复索引创建列并填充列值
- php - 取消链接文件权限被拒绝
- django - 部署 Django 应用程序后,Nginx 不从 /var/www/html 提供文件
- c++ - 如何打印包含整数和整数集的 std::maps?