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?
-
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?PatrickSteele– PatrickSteele2010-09-14 03:17:33 +00:00Commented 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.JoseMarmolejos– JoseMarmolejos2010-09-14 16:00:49 +00:00Commented Sep 14, 2010 at 16:00
-
1use this valueinjecter.codeplex.com if you like dynamic/convention based mappingOmu– Omu2010-09-17 16:45:22 +00:00Commented Sep 17, 2010 at 16:45
Add a comment
|
2 Answers
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.
1 Comment
JustMaier
Any way to do this with DynamicMap rather than CreateMap?