0

Bottom line, after reviewing comments:

Not possible with Mapster, and it's not Mapster's "mandate" - it belongs to business logic (before/after mapping).

Original question:

Is there a way to exclude/ignore records/entries according to their value?

For instance with:

public class Source
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Destination
{
    public string Name { get; set; }
    public string Number { get; set; }
}

and mapping:

TypeAdapterConfig<Source, Destination>.NewConfig()
    .Map(dest => dest.Name, src => src.Name)
    .Map(dest => dest.Number, src => src.Value);

Assuming I have:

var src = new List<Source>[]{
    new Source { Name = "one", Value = "1" },
    new Source { Name = "two", Value = "2" },
    new Source { Name = "three", Value = "3" }
};

And I want only records whose names have 3 letters or less (i.e. include one and two but not three), so output will be:

Destination { Name = "one", Number = "1" },
Destination { Name = "two", Number = "2" },

and Destination { Name = "three", Number = "3" } is left out.

Is there a way to accomplish it with Mapster?

Tar
  • 8,529
  • 9
  • 56
  • 127
  • 2
    _"Is there a way to accomplish it with Mapster?"_ - sounds a bit like XY-problem. Why do you want to do that? I would argue that this is not a mapping logic and should be separated from it. – Guru Stron May 15 '23 at 11:41
  • 1
    Mapster's should copy from one structure to another, it should not be the job of mapster to Filter such data and determine which to map and which not. To do that, you should use a Linq Query (or other method of your choice) to filter the data before then passing it to Mapster to convert. – jason.kaisersmith May 15 '23 at 11:54
  • 1
    @GuruStron: agree. Updated the question accordingly. Thanks. – Tar May 15 '23 at 12:04
  • @jason.kaisersmith: agree. Updated the question accordingly. Thanks. – Tar May 15 '23 at 12:04

0 Answers0