自动映射器:获取属性列表

时间:2019-06-27 13:35:02

标签: c# automapper

我需要从嵌套对象中获取属性列表。

那是我的代码的一部分:

O(s*t)

BD

[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; }
}

创建映射:

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 |

最后,我得到结果:

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()))

我需要在“ ListComplexCodes”字段中获取属性“ Code”,在我的情况下为“ COD_ART_05”,“ COD_ART_06”。

有人可以帮我吗?

谢谢。

0 个答案:

没有答案