I was getting the same error. From what you posted I don't know if our reasons are the same, but perhaps it helps explaining my error reason.
There is an overload with string as first parameter, but we need the other where the first parameter is a function. The compiler does not seem to get that. In my case, that was because the compiler had an error in the second argument. Once that was fixed, there was no complaining about "cannot convert lambda expression to type 'string' because it is not a delegate type"
anymore.
Here's the stuff I work with:
public class ClassA
{
public DateTime? memberX { get; set; }
public DateTime? memberY { get; set; }
}
public class ClassB
{
public long? memberX { get; set; }
public long? memberY { get; set; }
}
DateTime IntToDateTime(int t);
int DateTimeToInt(DateTime t);
The first (CreateMap<ClassA,ClassB>
) works, the second (CreateMap<ClassB,ClassA>
) doesn't.
CreateMap<ClassA, ClassB>()
.ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? DateTimeToInt(src.memberX.Value) : 0))
.ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? DateTimeToInt(src.memberY.Value) : 0))
;
CreateMap<ClassB, ClassA>()
.ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? IntToDateTime((int)src.memberX.Value) : null))
.ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? IntToDateTime((int)src.memberY.Value) : null))
;
What does work for the second (CreateMap<ClassB,ClassA>
) is this:
CreateMap<ClassB, ClassA>()
.ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? IntToDateTime((int)src.memberX.Value) : (DateTime?)null))
.ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? IntToDateTime((int)src.memberY.Value) : (DateTime?)null))
;
Are you getting any other compiler errors?
Possible solution:
Is your EventType
class the same class, or is one a Namespace1.SomeClass EventType
and the other a Namespace2.SomeClass EventType
? If that's the case, you need to create a map for those, first. Then it should work without .ForMember
.
Mapper.CreateMap<Namespace1.SomeClass, Namespace2.SomeClass>();
Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>();
That would be why the compiler cannot read the second argument in your ForMember
, resulting in the confusion what overload it should choose.