如何使用automapper映射包含类中列表的类

时间:2014-07-20 05:30:23

标签: asp.net-mvc-4 automapper

我有两个类,一个源和目标,每个类都包含列表。

来源类:CompanyResponse

[DataContract]
    public class CompanyResponse
        {
            [DataMember]
            public string CompanyId { get; set; }
            [DataMember]
            public string CompanyName { get; set; }
            [DataMember]
            public string CompanyDomain { get; set; }
            [DataMember]
            public int Subscription { get; set; }
            [DataMember]
            public DateTime? ActiveTill { get; set; }
            [DataMember]
            public List<Subscription> SubscriptionList { get; set; }
        }




[DataContract]
        public class Subscription
        {
            [DataMember]
            public int SubscriptionId { get; set; }
            [DataMember]
            public SubscriptionType SubscriptionType { get; set; }
            [DataMember]
            public string SubscriptionName { get; set; }
            [DataMember]
            public string SubscriptionCode { get; set; }
            [DataMember]
            public Nullable<decimal> SubscriptionPriceYearly { get; set; }
        }
        public enum SubscriptionType : int
        {
            AvailableOnline = 1,
            Custom = 2
        }

目的地类:

public class CreateCompanyViewModel
    {

        public int CompanyId { get; set; }
        public string CompanyName { get; set; }
        public string CompanyDomain { get; set; }
        public int SubscriptionId { get; set; }
        public DateTime? CompanyActiveTill { get; set; }
        public List<SubscriptionViewModel> lstSubscription { get; set; }
    }

SubscriptionViewModel:

public class SubscriptionViewModel
    {
        public int SubscriptionId { get; set; }
        public SubscriptionType SubscriptionType { get; set; }
        public string SubscriptionName { get; set; }
        public string SubscriptionCode { get; set; }

    }
   public enum SubscriptionType : int
   {
       AvailableOnline = 1,
       Custom = 2
   }

我想使用automapper映射这两个类。我编写了以下代码

Mapper.CreateMap<CompanyResponse, CreateCompanyViewModel>();
CompanyResponse objCompanyResponse = _objCompanyproxy.GetCompanyInformation();
CreateCompanyViewModel objCreateCompanyViewModel = new CreateCompanyViewModel();
objCreateCompanyViewModel  = Mapper.Map<CompanyResponse, CreateCompanyViewModel>(objCompanyResponse);

但它没有正确映射。请帮我如何映射两个类。

1 个答案:

答案 0 :(得分:0)

您可以使用此通用方法映射列表并将其分配给主对象。

public static List<Toutput> MapObjectsList<TInput, Toutput>(List<TInput> inputObj)
        {
            var data = new List<Toutput>();
            foreach (var row in inputObj)
            {
                data.Add(Mapper.Map<Toutput>(row));
            }

            return data;
        }

你可以像这样使用它:

Mapper.CreateMap<CompanyResponse, CreateCompanyViewModel>();
Mapper.CreateMap<Subscription, SubscriptionViewModel>();
        CompanyResponse objCompanyResponse = _objCompanyproxy.GetCompanyInformation();
        CreateCompanyViewModel objCreateCompanyViewModel = new CreateCompanyViewModel();
    objCreateCompanyViewModel  = Mapper.Map<CompanyResponse, CreateCompanyViewModel>(objCompanyResponse);
        objCreateCompanyViewModel.lstSubscription = MapObjectsList<Subscription,SubscriptionViewModel>(objCompanyResponse.SubscriptionList.toList());