I have two identical models:
Model 1
public class Modelo
{
public string IdModelo { get; set; }
public List<DatosModelo> Datos { get; set; }
}
public class DatosModelo
{
public string IdModelo { get; set; }
public string Title { get; set; }
public string Value { get; set; }
}
Model 2
public class ModeloBD
{
public string IdModelo { get; set; }
public List<DatosModeloBD> Datos { get; set; }
}
public class DatosModeloBD
{
public string Title { get; set; }
public string Value { get; set; }
}
To convert model 2 to model 1 I have tried with:
List<ModeloBD> data = _svcConfiguracion.GetOperaciones(4);
List<Modelo> transformar = data.Select(ToVm());
public Modelo ToVm(ModeloBD modelo)
{
return new Modelo
{
IdModelo = modelo.IdModelo,
Datos = modelo.Datos.Select(ToVm) // Intento aqui realizar la transformacion de la lista de datos y falla
};
}
public DatosModelo ToVm(DatosModeloBD datos)
{
return new DatosModelo
{
Title = datos.Title,
Value = datos.Value
};
}
Is there a way to do it without having to split it?
The best way to convert data structures is using the library
AutoMapper
AutoMapper nuget
with this libraries you could define how the entities are converted
Getting StartedGuide
then you define the mapping
to be able to use the
Map<>()
and convert from one type to another mapped