How to convert from List<> to IList< > using Automapper?

I need to convert with automapper a List to IList knowing that both are in different classes and one of them has constructor. Is that possible? I am using Automapper 6.2.2.

public class Pessoa
{
    public int Id { get; set; }
    public int Nome { get; set; }   
}

public class MinhasPessoas
{
    public List<Pessoa> Pessoas { get; set; }
}

public abstract class RegisterNewPessoas
{
    public IList<Pessoa> Pessoas { get; set; }

    RegisterNewPessoas(IList<Pessoa> pessoas)
    {
        Pessoas = pessoas;
    }
}

Mapping:

CreateMap<MinhasPessoas, RegisterNewPessoa>()
    .ConstructUsing(ps => new RegisterNewPessoa(//Converter as listas aqui));
Author: Master JR, 2019-02-22

1 answers

Does not need to do any conversion. List<> implements IList<>, so any instance of List<> can be assigned to a variable IList<>.

I don't even know if it is necessary to use the ConstructUsing method in this case. If it is, just do the assignment.

ConstructUsing(ps => new RegisterNewPessoa { Pessoas = ps.Pessoas }

See a functional example in .NET Fiddle.

 2
Author: LINQ, 2019-02-22 11:37:44