c# - 如果字符串不为空,则条件映射(ASP.NET Core)
问题描述
我在我的项目中使用 Automapper
在映射器中,我将字符串映射到ICollection
.
这是我的做法
.ForMember(x => x.PropertyImages,
opt => opt.MapFrom(aa => aa.Attachments.Split(';', StringSplitOptions.None).ToList()));
但是如果字符串为空。我有错误
对象未设置为对象的实例
仅当字符串不为空时,我如何进行条件映射
解决方案
您可以使用三元运算符来检查字符串
.ForMember(x => x.PropertyImages,
opt => opt.MapFrom(aa => !string.IsNullOrEmpty(aa.Attachments) ? aa.Attachments.Split(';', StringSplitOptions.None).ToList() : new List<string>()));
推荐阅读
- java - joda 时间与 java 时间的默认语言环境
- r - 如何仅在 geom_label_repel() 中更改文本颜色而不是线条颜色
- javascript - 使用 SQLite 反应原生博览会
- javascript - 在 WEBSITE 中以 BTC 实时显示美元金额的脚本
- three.js - WebGL:将分数布朗运动烘焙到纹理中
- python - 尝试运行使用 pyinstaller 创建的 Mac .app 时如何解决 Matplotlib 模块未找到错误
- apache - 任意位置的端口转发
- php - 如何在laravel刀片中从json解码中回显键名和键值
- wordpress - 在具有特定值的字段的 10 个条目后禁用 Gravity 表单的提交表单
- android - 使用 mokk 时收到“每个 { ... } 块内缺少呼叫”