Question

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);
Was it helpful?

Solution

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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top