Aleksej_Shherbak
Aleksej_Shherbak

Reputation: 3078

Map List<Class> to List<Class> with automapper for .net core

I have two models. The first is:

public class Push
{
    public int PushId { get; set; }
    public Customer Customer { get; set; }
    public int CustomerId { get; set; }
    public string PackageId { get; set; }
    public string Category { get; set; }
    public string Advertiser { get; set; }
    public PushTemplate PushTemplate { get; set; }
    public string TemplateType { get; set; }
    public string IntervalType { get; set; }
    public int TopDateTimeBorder{ get; set; }
    public int  BottomDateTimeBorder { get; set; }
    public DateTime ClientStartDateTime { get; set; }
    public string LangCode { get; set; }
    public string PushTitle { get; set; }
    public string PushBody { get; set; }
    public string FCMResponse { get; set; }
    public bool Sent { get; set; }
    public DateTime CreatedAt { get; set; }
    public Distribution Distribution { get; set; }
    public Push()
    {
        CreatedAt = DateTime.UtcNow;
    }
}

and the second is:

public class FailedPush
{
    public int FailedPushId { get; set; }

    public Customer Customer { get; set; }
    public int CustomerId { get; set; }
    public string PackageId { get; set; }
    public string Category { get; set; }
    public string Advertiser { get; set; }
    public PushTemplate PushTemplate { get; set; }
    public string TemplateType { get; set; }
    public string IntervalType { get; set; }
    public int TopDateTimeBorder{ get; set; }
    public int  BottomDateTimeBorder { get; set; }
    public DateTime ClientStartDateTime { get; set; }
    public string LangCode { get; set; }
    public string PushTitle { get; set; }
    public string PushBody { get; set; }
    public string FCMResponse { get; set; }
    public DateTime CreatedAt { get; set; }
    public Distribution Distribution { get; set; }
    public FailedPush()
    {
        CreatedAt = DateTime.UtcNow;
    }
}

I would like to get a list of FailedPush models from a list of Push models if the property Sent is false.

So I have the following code for this:

 var failedPushes = _mapper.Map<List<Push>, List<FailedPush>>(await pushes.Where(x => !x.Sent).ToListAsync()); 

But failedPushes is empty. To inject mapper I user IMapper interface:

// class prop 
private readonly IMapper _mapper;

... 

// constructor 

public ScopedProcessingService(IMapper mapper) 
{ 
...
_mapper = mapper;
...
}

And in the Startup.cs class I have the following:

services.AddAutoMapper(typeof(Startup));

So what is wrong?

Upvotes: 0

Views: 4268

Answers (1)

Andr&#233; Sanson
Andr&#233; Sanson

Reputation: 440

You have to create a configuration of your mapping classes.

Automapper will automatically search for classes that inherit Profile to create the configuration.

Create a folder Profile and put your configurations there.

public class PushProfile : Profile
{
    public PushProfile()
    {
        CreateMap<Push, FailedPush>();
    }
}

Upvotes: 2

Related Questions