Eugene Sukh
Eugene Sukh

Reputation: 2737

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

I using Automapper in my project

In mapper, I map the string to ICollection.

Here is how I do it

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

But if string is empty. I got error

object not set to an instance of an object

How I can make conditional mapping, only if string not null

Upvotes: 0

Views: 880

Answers (1)

Anton
Anton

Reputation: 851

You can use ternary operator to check string


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

Upvotes: 1

Related Questions