C
C#2y ago
KuumaKoira

AutoMapper - map object from source to destionation's list of objects.

Any way I can map source object (e.g string) inside destination list of objects (e.g List<string>) using AutoMapper? simplified code example of what I want to achieve:
using AutoMapper;

var config = new MapperConfiguration(cfg => cfg.CreateMap<SourceModel, DestinationModel>()
.ForPath(dest => dest.SubModel.SomeString, opt => opt.MapFrom(src => src.SomeString))
.ForPath(dest => dest.SubModel.SomeOtherString, opt => opt.MapFrom(src => src.SomeOtherString))
.ForPath(dest => dest.SubModel.ListOfStrings, opt => opt.MapFrom(src => src.ListOfStrings))); // + inside destination's ListOfStrings I also want to map source's SomeOtherString so that resulting destination's ListOfStrings is source's ListOfString + source's SomeOtherString inside the dest list

class SourceModel
{
public string SomeString { get; set; }
public string SomeOtherString { get; set; }
public List<string> ListOfStrings { get; set; }
}

class DestinationModel
{
public SubModel SubModel { get; set; }
}

class SubModel
{
public string SomeString { get; set; }
public string SomeOtherString { get; set; }
public List<string> ListOfStrings { get; set; } //this list should also include SomeOtherString value in it after mapping

}
using AutoMapper;

var config = new MapperConfiguration(cfg => cfg.CreateMap<SourceModel, DestinationModel>()
.ForPath(dest => dest.SubModel.SomeString, opt => opt.MapFrom(src => src.SomeString))
.ForPath(dest => dest.SubModel.SomeOtherString, opt => opt.MapFrom(src => src.SomeOtherString))
.ForPath(dest => dest.SubModel.ListOfStrings, opt => opt.MapFrom(src => src.ListOfStrings))); // + inside destination's ListOfStrings I also want to map source's SomeOtherString so that resulting destination's ListOfStrings is source's ListOfString + source's SomeOtherString inside the dest list

class SourceModel
{
public string SomeString { get; set; }
public string SomeOtherString { get; set; }
public List<string> ListOfStrings { get; set; }
}

class DestinationModel
{
public SubModel SubModel { get; set; }
}

class SubModel
{
public string SomeString { get; set; }
public string SomeOtherString { get; set; }
public List<string> ListOfStrings { get; set; } //this list should also include SomeOtherString value in it after mapping

}
0 Replies
No replies yetBe the first to reply to this messageJoin