20

I'm using Mapper.DynamicMap() inside a generic method and would like to, without using .CreateMap(), ignore some any source values that are null. Is this even possible?

3
  • Do you mean if you had a list of "source" objects and some were null, you don't want a list of mapped "destination" objects with some nulls -- you just want the non-null source objects mapped? Commented Sep 14, 2010 at 3:17
  • Exactly. For example: if I have a Source object with Name and SSN, and a Destination object with the same property, if any of those properties are null in the Source object i don't want them mapped in the Destination object. Why you may ask? I don't any properties that are already set in the Destination object to be overwritten by null values. Commented Sep 14, 2010 at 16:00
  • 1
    use this valueinjecter.codeplex.com if you like dynamic/convention based mapping Commented Sep 17, 2010 at 16:45

2 Answers 2

32

If you want all source properties with null values to be ignored you could use:

Mapper.CreateMap<SourceType, DestinationType>()
                    .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

Otherwise, you can do something similar for each member. This will get quit tedious if there are a large number of properties.

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

1 Comment

Any way to do this with DynamicMap rather than CreateMap?
0

I solved it with DataMember property in destination type [DataMember(EmitDefaultValue = false)] add this in the destination DTO

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.