0

I'm using automapper 13.0.1 to map one object to another:

CreateMap<CandidateInfoContactDataSendEvent, TechUser>()
.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.NewPhoneNumber))
.ForMember(dest => dest.EmailHash, opt => opt.MapFrom<TechUserEmailHashResolver>())
.ForMember(dest => dest.BirthDate, opt => opt.Condition((src, dest) => dest.DestroyDate == null))
.ForMember(dest => dest.AdditionalPhoneNumber, opt => opt.Condition((src, dest) => dest.DestroyDate == null))
.ForMember(dest => dest.FirstName, opt => opt.Condition((src, dest) => dest.DestroyDate == null))
.ForMember(dest => dest.LastName, opt => opt.Condition((src, dest) => dest.DestroyDate == null))
.ForMember(dest => dest.FatherName, opt => opt.Condition((src, dest) => dest.DestroyDate == null))
.ForMember(dest => dest.Gender, opt => opt.Condition((src, dest) => dest.DestroyDate == null))
.ForMember(dest => dest.Email, opt => opt.Condition((src, dest) => dest.DestroyDate == null));

As you can see some properties maps with the same condition. And I want to combine this conditions in one. I cant not use ForAllMembers for PhoneNumber and EmailHash because they do not depend on DestroyDate. But I need something like ForAllMembers.I need ForAllMembers condition except PhoneNumber and EmailHash.

Thanks for help

1
  • 3
    You have access to the destination member inside ForAllMembers, so you can filter for that. Commented Aug 5 at 8:11

1 Answer 1

1

Credit to @LucianBarganou's comment, you can provide the Action<IMemberConfigurationExpression> as parameter to the ForAllMembers function.

With IMemberConfigurationExpression, you can access the DestinationMember to get the Name and set the Condition mapping rule.

It should be as below:

cfg.CreateMap<CandidateInfoContactDataSendEvent, TechUser>()
    .ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.NewPhoneNumber))
    .ForMember(dest => dest.EmailHash, opt => opt.MapFrom<TechUserEmailHashResolver>())
    .ForAllMembers(opt => 
    {
        if (opt.DestinationMember.Name != nameof(TechUser.PhoneNumber)
            && opt.DestinationMember.Name != nameof(TechUser.EmailHash))
        {
            opt.Condition((src, dest) => dest.DestroyDate == null);
        }
    });

Demo @ .NET Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.