问题
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