0

I am newbie for Automapper.

I have a domin and ViewModel class.

I want to map the domain class to ViewModel class.

Domain Class :

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

    }

View Model Class

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

    }

Now, If you can see , I can map ID,PONo,PoDate perfectly.

But, I want to know how can I map the CompanyID and ComapanyName from Domain to VM class ?

Transform Method

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

        }
bnil
  • 1,531
  • 6
  • 35
  • 68

1 Answers1

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


private Company GetCompanyById(int companyId)
    {
        ....
    }
severin
  • 5,203
  • 9
  • 35
  • 48