在AutoMapper中映射子列表属性上的“计数" [英] Mapping the 'count' on child list property in AutoMapper

查看:93
本文介绍了在AutoMapper中映射子列表属性上的“计数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.NET MVC 4应用程序中工作,对于我的域也有类似的东西.

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

我想将其映射到如下所示的PartyVM:

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

在进行映射时,如何告诉AutoMapper计算列表的计数?我有这个,但是所有要做的就是告诉它忽略!

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

谢谢.

推荐答案

AutoMapper支持开箱即用,您的目标成员名称不正确.如果您将PartyVM属性命名为"CakeCandlesCount",它将具有正确的值.

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.

这篇关于在AutoMapper中映射子列表属性上的“计数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆