I have an EF object (Assay) that i am mapping to a data transfer object (AssayDTO) using automapper. I am able to get the AssayValue, AssayCode, AssayName, and AssayComments properties populated by using
Mapper.Map<IEnumerable<Assay>, IEnumerable<AssayDTO>>(assays);
how can I populate the AssayDTO Genes property using automapper, when this value would be a separate query based on the AssayID I want the AssayDTO Genes property to contain the list of genes for each Assay
public partial class Assay
{
public int AssayID { get; set; }
public string AssayCode { get; set; }
public string AssayName { get; set; }
public string AssayComments { get; set; }
public long ModifID { get; set; }
public byte[] LAST_UPDATED_TIMESTAMP { get; set; }
}
public class AssayDTO
{
public string AssayValue { get; set; }
public string AssayCode { get; set; }
public string AssayName { get; set; }
public string AssayComments { get; set; }
public IEnumerable<string> Genes { get; set; }
}
IEnumerable<AssayDTO> dto = null;
var assays = _assayRepository.GetAssays();
Mapper.CreateMap<Assay, AssayDTO>()
dto = Mapper.Map<IEnumerable<Assay>, IEnumerable<AssayDTO>>(assays);
You can configure the mapping like so:
Mapper.CreateMap<Assay,AssayDto>()
.ForMember(dto =>
dto.Genes,
m => m.MapFrom(a => GenesList // your separate list of genes
.Where(g => g.AssayID == a.AssayID)
.Select(g => g.Name)));
BTW, personally I always prefer this shape of statements:
dtos = assays.Select(a => Mapper.Map<AssayDTO>(a));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.