From what I've read from this error, f.E. here: Why are some object properties UnaryExpression and others MemberExpression?
This happens, when an object is expected, but an value type is returned, so the CLR has to pack this, which is another (Unary)expression.
What really bothers me, the following AutoMapper-Mapping works without problems:
.ForMember(d => d.IndividualId, c => c.MapFrom(f => f.Individual.Id));
It only doesn't work, when the Mapping-Expression has another expression, which returns a Value Type:
.ForMember(d =>
d.IndividualId, c => c.MapFrom(f =>
f.Individuals.First(d => d.Individual.Name == "Test").Id
));
I wrote this example just to show, what I'd like to do, so it might not be 100% appropriate? I just can't get behind, why the first Expression doesn't cause this exception, because in both cases an packing has to happen?
Edit
Itvan's answer works as well, the goal is just to remove the need for the wrapping. This works with something like this too:
m => m.MapFrom(f =>
f.Individuals.Where(ms => ms.Individual.Name == name)
.Select(i => i.Individual.Id).FirstOrDefault()
)
I've just got the same exception and it may be a bug in the AutoMapper, I'm not sure, but I have a workaround after hours. This is what I have: