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()
)
2

There are 2 answers

1
ltvan On BEST ANSWER

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:

class MyDto
{
    public int? StatusId;
    public int? OtherStatusId;
}
class MyModel
{
    public int StatusId; 
}

// this should work normally
.ForMember(d => d.StatusId, c => c.MapFrom(f => f.Order.StatusId));
// this causes the exception above, but I don't know why, 
// maybe because I have some quite complex mapping
.ForMember(d => d.OtherStatusId, c => c.MapFrom(f => f.Other.StatusId));

// apply a cast on the source expression make the mapping smoothly
.ForMember(d => d.OtherStatusId, c => c.MapFrom(f => (int?)f.Other.StatusId));
0
Jaider On

I think some version of AutoMapper has this problem. I am not sure if this still happening on the latest version???

But the main issue it's the Nullable Expression that is not so clear to be resolved. It is easier for AutoMapper to return a simple value like: c => c.Id than resolve a Nullable Expression like c => c.Data.Path2.Data.Path2.Where(..).Id. One way to solve this could be check for null (old way)... an expression tree lambda may not contain a null propagating operator

By the way, This code is bad if you are trying to use AutoMapper for DB entities.... I will suggest to use ProjectTo and send a parameter with the list of IndividualIds. More info: http://docs.automapper.org/en/stable/Queryable-Extensions.html