0

我是 Automapper 的新手。

我有一个域和 ViewModel 类。

我想将域类映射到 ViewModel 类。

域类:

 public class PurchaseOrder
    {
        public int Id { get; set; }
        public string PONo { get; set; }
        public DateTime PODate { get; set; }

        public int CompanyId { get; set; }
    }

    public class Company
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

查看模型类

 public class PurchaseOrderVM
    {
        public int Id { get; set; }
        public string PONo { get; set; }
        public DateTime PODate { get; set; }

        public int CompanyId { get; set; }
        public string CompanyName { get; set; }


    }

    public class CompanyVM
    {
       public int Id { get; set; }
       public string Name { get; set; }

    }

现在,如果你能看到,我可以完美地映射 ID、PONo、PoDate。

但是,我想知道如何将 CompanyID 和 ComapanyName 从 Domain 映射到 VM 类?

变换方法

static void POTransform()
        {


            PurchaseOrder PODomain = new PurchaseOrder();

            PODomain.Id = 1;
            PODomain.PONo = "154";
            PODomain.PODate = Convert.ToDateTime("5-Jun-2014");

            Mapper.CreateMap<PurchaseOrder, PurchaseOrderVM>()
                .ForMember(dest => dest.CompanyName, Source => Source.MapFrom(????);

        }
4

1 回答 1

0
Mapper.CreateMap<PurchaseOrder, PurchaseOrderVM>()
            .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => GetCompanyById(src.CompanyId).Name));
    }


private Company GetCompanyById(int companyId)
    {
        ....
    }
于 2014-10-21T08:48:59.230 回答