MrJahanbin
MrJahanbin

Reputation: 465

Mapster - Ignoring the value 0 in non-nullable numeric variables in Mapster

Ignoring the value 0 in non-nullable numeric variables in Mapster

I want to create a very simple mapping. My class is as follows:

public class C
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

Here's a sample value and test:

C cat = new C() { Age = 8 };
C cat2 = new C() { Id = 2, Name = "Jana", Age = 20 };

TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true);

var output = cat.Adapt(cat2);

It ignores null values, but it doesn't ignore numeric values that are 0, which breaks the mapping. Does anyone have a solution for this?

Upvotes: 1

Views: 388

Answers (1)

darrenleeyx
darrenleeyx

Reputation: 492

You can ignore members conditionally https://github.com/MapsterMapper/Mapster/wiki/Ignoring-members#ignore-conditionally

C cat = new C() { Age = 8 };
C cat2 = new C() { Id = 2, Name = "Jana", Age = 20 };

TypeAdapterConfig<C, C>.NewConfig()
    .IgnoreIf((src, dest) => src.Id == 0, dest => dest.Id)
    .IgnoreNullValues(true);

var output = cat.Adapt(cat2);
// Age = 8, Id = 2, Name = "Jana"

Upvotes: 0

Related Questions