Mapping the 'count' on child list property in AutoMapper

时间秒杀一切 提交于 2019-12-02 21:49:52

问题


I am working in an ASP.NET MVC 4 application, and have something similar to this for my domain.

public class Party
{
    public Cake PartyCake { get; set; }
    public List<Candles> CakeCandles { get; set; }
}

I want to map that to the PartyVM which looks like so:

public class PartyVM
{
    public string PartyCakeName { get; set; }
    public int CandlesCount { get; set; }
}

How can I tell AutoMapper to work out the Count of the list when doing its mappings? I have this, but all this does is tell it to ignore!

Mapper.CreateMap<Party, PartyVM>()
            .ForMember(dest => dest.CandlesCount, opt => opt.Ignore());

Thanks.


回答1:


AutoMapper supports Count out of the box, your destination member is just named incorrectly. If you name your PartyVM property "CakeCandlesCount" it will have the right value.



来源:https://stackoverflow.com/questions/22400687/mapping-the-count-on-child-list-property-in-automapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!