I need to get a list of properties from nested Object.
That's part of my code:
[Table("Articles")]
class Article
{
public Article()
{
ListComplex = new List<ArticlesComplex>();
}
public long Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public virtual ICollection<ArticlesComplex> ListComplex { get; set; }
}
[Table("ArticlesComplex")]
class ArticlesComplex
{
public long ArticleId { get; set; }
public virtual Article Article { get; set; }
public long ComplexId { get; set; }
public virtual Article Complex { get; set; }
}
//***********************
// DTO
//***********************
public class ArticleDTO
{
public ArticleDTO()
{
ListComplex = new List<long>();
ListComplexCodes = new List<string>();
}
public long Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public IEnumerable<long> ListComplex { get; set; }
public IEnumerable<string> ListComplexCodes { get; set; }
}
BD
ARTICLES ARTICLESCOMPLEX
Id | Code | Name | ArticleId | ComplexId
5 | COD_ART_05 | Paper | 7 | 5
6 | COD_ART_06 | Item | 7 | 6
7 | COD_ART_07 | Pendrive |
Create the mapping:
mapper.CreateMap<Article, ArticleDTO>().ForMember(m => m.ListComplex, m => m.MapFrom(y => y.ListComplex.Select(x => x.Complex.Id).ToList()))
.ForMember(m => m.ListComplexCodes, m => m.MapFrom(y => y.ListComplex.Select(x => x.Complex.Codigo).ToList()))
Finally, I get the result:
var dbWhere = DbContext.Articles.Where(x => x.Codigo == "COD_ART_07");
var list = await dbWhere.ProjectTo<ArticleDTO>(null, "ListComplex").ToListAsync();
// list.ListComplex (2) -> 5,6
// list.ListComplexCodes (0) ???
I would need to get the property 'Code', in my case "COD_ART_05", "COD_ART_06" in the field 'ListComplexCodes'.
Could anyone help me with this?
Thanks.