string not null 条件映射(ASP.NET核心)

Conditional mapping if string not null (ASP.NET Core)

我在我的项目中使用 Automapper

在映射器中,我将字符串映射到 ICollection

这是我的做法

.ForMember(x => x.PropertyImages,
                opt => opt.MapFrom(aa => aa.Attachments.Split(';', StringSplitOptions.None).ToList()));

但是如果字符串为空。我收到错误

object not set to an instance of an object

如何进行条件映射,前提是字符串不为空

您可以使用三元运算符来检查字符串


.ForMember(x => x.PropertyImages,
      opt => opt.MapFrom(aa => !string.IsNullOrEmpty(aa.Attachments) ? aa.Attachments.Split(';', StringSplitOptions.None).ToList() : new List<string>()));