Reputation: 1931
I have the following mapping:
Mapper.Initialize(cfg => cfg.CreateMap<StructureDTO, Structure>()
.ForMember(dest => dest.ParentId,
opt => opt.MapFrom(src => src.ParentStructureId != Guid.Empty ? src.ParentStructureId : (Guid?)null)))
dest.ParentId is nullable Guid
src.ParentStructureId is Guid
I get the following exception:
An exception of type 'System.InvalidOperationException'
occurred in System.Core.dll
but was not handled in user code
Additional information: The operands for operator 'NotEqual'
do not match the parameters of method 'op_Inequality'
.
Is this because I'm trying to map two different types? I'm at a loss here.
Automapper version is 5.0.2
Upvotes: 0
Views: 1742
Reputation: 775
make a method which does the work for you, then use the method in the ForMember
method:
private Guid? Transform(StructureDTO src) {
return src.ParentStructureId != Guid.Empty ? src.ParentStructureId : (Guid?) null;
}
then do your mapping this way:
Mapper.Initialize(cfg => cfg.CreateMap<StructureDTO, Structure>()
.ForMember(dest => dest.ParentId,
opt => opt.MapFrom(src => Transform(src))));
This is an error of AutoMapper's expression building logic, it tries to convert your lambda into a .NET expression tree
, to bypass this we fore it to make a MethodCallExpression
. This calls your new method directly without converting it into expressions.
Upvotes: 1