1

I'm using automapper v11 (C# net6) I would like to Ignore(not map) all null values for all fields.

Here an example

class SrcExample
{
    public string? Name { get; set; }
    public int? Age { get; set; }
}

class DestExample
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var map = CreateMap<SrcExample, DestExample>();
map.ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));

I'm trying to Ignore(not map) all null values using opts.Condition but it only works for strings.

For int? I got srcMember = 0 instead of srcMember = null. Seems like Automapper uses default int value as srcMember value.

Why Automapper behaves this way?

Is there a way (using automapper) to Ignore(not map) all null values for all fields.

This question (How to ignore null values for all source members during mapping in Automapper 6?) do not resolve the problem, due to the set method on the destination class is called anyway, (Here an example of how set method is called https://dotnetfiddle.net/1grZkZ) I want to avoid set method being called. I want to completely ignore the map when the source value is null.

satellite satellite
  • 893
  • 2
  • 10
  • 27
  • In your destination class the `Age` property is `int` instead of `int?`. `int` default value is 0. – Dimitris Maragkos Oct 15 '22 at 22:02
  • @DimitrisMaragkos Thanks for the comment. The source class is `int?` and the value of `Age` is `null`, so `srcMember` should be `null` not 0 on the `Condition`. I want to not map if the source value is null. The destination could be anything, right? – satellite satellite Oct 16 '22 at 01:08
  • Set `UseDestinationValue` for `Age`. – Lucian Bargaoanu Oct 16 '22 at 05:25
  • @LucianBargaoanu I want to not map all properties with values = `null`, not just `Age`. Could you please give an example of how could I use `UseDestinationValue` in order to do that. Thanks!!! – satellite satellite Oct 16 '22 at 13:44
  • Why did this question get a -1? Isn't it clear enough? Is there a simple solution? (Which by the way nobody has answered it) I did a research and I couldn't find a solution for this. Not sure why this questions has -1. Any help please? – satellite satellite Oct 16 '22 at 13:49
  • Does this answer your question? [How to ignore null values for all source members during mapping in Automapper 6?](https://stackoverflow.com/questions/43947475/how-to-ignore-null-values-for-all-source-members-during-mapping-in-automapper-6). Second answer has solution for exactly your problem. – Dimitris Maragkos Oct 16 '22 at 14:43
  • And here is a dotnetfiddle example: https://dotnetfiddle.net/WrfKgm – Dimitris Maragkos Oct 16 '22 at 16:05
  • @DimitrisMaragkos Thanks for the comment. Here the same dotnetfiddle example you gave me (with a Console.WriteLine on the `set` methods): https://dotnetfiddle.net/1grZkZ The `set` method for `int` (`Age`) is still called, and that is want I want to avoid. I want to ignore(no map) null values. – satellite satellite Oct 16 '22 at 22:14

0 Answers0