AutoMapper:无法将类型 'System.Linq.Expressions.MethodCallExpressionN' 的对象转换为类型 'System.Linq.Expressions.MemberExpression'
AutoMapper: Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.MemberExpression'
根据我从这个错误中读到的内容,f.E。这里:
Why are some object properties UnaryExpression and others MemberExpression?
当需要一个对象时会发生这种情况,但会返回一个值类型,因此 CLR 必须打包它,这是另一个(一元)表达式。
真正困扰我的是,以下 AutoMapper-Mapping 可以正常工作:
.ForMember(d => d.IndividualId, c => c.MapFrom(f => f.Individual.Id));
只有当 Mapping-Expression 有另一个表达式时,它才不起作用,returns 一个值类型:
.ForMember(d =>
d.IndividualId, c => c.MapFrom(f =>
f.Individuals.First(d => d.Individual.Name == "Test").Id
));
我写这个例子只是为了展示我想做什么,所以它可能不是 100% 合适?我就是跟不上,为什么第一个表达式不会导致这个异常,因为在这两种情况下都必须进行打包?
编辑
答案也有效,目标只是消除包装的需要。这也适用于这样的东西:
m => m.MapFrom(f =>
f.Individuals.Where(ms => ms.Individual.Name == name)
.Select(i => i.Individual.Id).FirstOrDefault()
)
我刚遇到同样的异常,它可能是 AutoMapper 中的一个错误,我不确定,但我在下班后有一个解决方法。这是我的:
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));
我认为某些版本的AutoMapper 有这个问题。我不确定这是否仍然发生在最新版本上???
但主要问题是 Nullable Expression 不是很明确,无法解决。对于 AutoMapper 而言,return 一个简单的值(如 c => c.Id
)比解析一个可空表达式(如 c => c.Data.Path2.Data.Path2.Where(..).Id
)更容易。解决这个问题的一种方法是检查 null(旧方法)...
顺便说一句,如果您尝试将 AutoMapper 用于数据库实体,则此代码很糟糕...我建议使用 ProjectTo
并发送一个带有 IndividualIds 列表的参数。
更多信息:http://docs.automapper.org/en/stable/Queryable-Extensions.html
根据我从这个错误中读到的内容,f.E。这里: Why are some object properties UnaryExpression and others MemberExpression?
当需要一个对象时会发生这种情况,但会返回一个值类型,因此 CLR 必须打包它,这是另一个(一元)表达式。
真正困扰我的是,以下 AutoMapper-Mapping 可以正常工作:
.ForMember(d => d.IndividualId, c => c.MapFrom(f => f.Individual.Id));
只有当 Mapping-Expression 有另一个表达式时,它才不起作用,returns 一个值类型:
.ForMember(d =>
d.IndividualId, c => c.MapFrom(f =>
f.Individuals.First(d => d.Individual.Name == "Test").Id
));
我写这个例子只是为了展示我想做什么,所以它可能不是 100% 合适?我就是跟不上,为什么第一个表达式不会导致这个异常,因为在这两种情况下都必须进行打包?
编辑
m => m.MapFrom(f =>
f.Individuals.Where(ms => ms.Individual.Name == name)
.Select(i => i.Individual.Id).FirstOrDefault()
)
我刚遇到同样的异常,它可能是 AutoMapper 中的一个错误,我不确定,但我在下班后有一个解决方法。这是我的:
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));
我认为某些版本的AutoMapper 有这个问题。我不确定这是否仍然发生在最新版本上???
但主要问题是 Nullable Expression 不是很明确,无法解决。对于 AutoMapper 而言,return 一个简单的值(如 c => c.Id
)比解析一个可空表达式(如 c => c.Data.Path2.Data.Path2.Where(..).Id
)更容易。解决这个问题的一种方法是检查 null(旧方法)...
顺便说一句,如果您尝试将 AutoMapper 用于数据库实体,则此代码很糟糕...我建议使用 ProjectTo
并发送一个带有 IndividualIds 列表的参数。
更多信息:http://docs.automapper.org/en/stable/Queryable-Extensions.html