我有一个EF对象(Assay),我使用automapper映射到数据传输对象(AssayDTO)。我可以使用
填充AssayValue,AssayCode,AssayName和AssayComments属性Mapper.Map<IEnumerable<Assay>, IEnumerable<AssayDTO>>(assays);
如何使用automapper填充AssayDTO Genes属性,此值将是基于AssayID的单独查询 我希望AssayDTO Genes属性包含每个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);
答案 0 :(得分:1)
您可以像这样配置映射:
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)));
顺便说一下,我个人总是喜欢这种形式的陈述:
dtos = assays.Select(a => Mapper.Map<AssayDTO>(a));