1

I am looking to map a entity named PortalUser to another entity named UserFacade, both of them have same fields except one because name of the field is different in both classes.

After a bit of googling I found out a way of mapping fields with different name to each other as stated here How to specify mapping rule when names of properties differ

The solution is to use ForMember function and explicitly define which field to map like stated in answer of question above.

My problem is that ForMember isn't working to way it is explained in almost every answer at stackoverflow

AutoMapper.Mapper.CreateMap(user.GetType(), typeof(UserFacade))
    .ForMember(dest => dest.PortalRole, opt => opt.MapFrom(src => src.Role);

var userFacade = AutoMapper.Mapper.Map<UserFacade>(user);

The 2nd line at ForMember says that cannot convert lambda to string type. The ForMember function is used the same way in almost every answer at stackoverflow, but it doesn't seem to work here, kindly help.

Community
  • 1
  • 1
mfs
  • 3,984
  • 6
  • 32
  • 51

2 Answers2

2

Try like below:

AutoMapper.Mapper.CreateMap(user.GetType(), typeof(UserFacade))
    .ForMember(dest => dest.PortalRole, opt => opt.MapFrom(src => src.Role));

Also can you confirm, this is added to namespace:

    using System.Linq;
Vinoth
  • 2,419
  • 2
  • 19
  • 34
  • This was a type in my question, this is how I am trying actually, and its isn't working – mfs Nov 25 '16 at 06:02
  • Error 31 Cannot convert lambda expression to type 'string' because it is not a delegate type D:\IRIS5\Main\CAP\Web\Cap.Web.Common\SessionState\UserFacade.cs 136 16 Cap.Web.Common – mfs Nov 25 '16 at 06:26
  • As i updated, can you ensure System.Linq namespace is added? – Vinoth Nov 25 '16 at 06:28
2

When I look at this. I see 3 possible reasons :

1) Typo in the question :

AutoMapper.Mapper.CreateMap(user.GetType(), typeof(UserFacade))
    .ForMember(dest => dest.PortalRole, opt => opt.MapFrom(src => src.Role));

2) usings

using AutoMapper;
using System.Linq;

3) no mapper for Role to PortalRole.