【发布时间】:2015-04-30 06:39:54
【问题描述】:
我正在尝试将包含此 Expression 的 Linq-to-Entity 运行到实体框架中。
不工作:
//2 seperated expressions, 1st expression calling the 2nd expression
public Expression<Func<User, UserDto>> UserToDtoExpr() {
var expr = AddressToDtoExpr();
return x => new UserDto() {
Id = x.Id,
Name = x.Name,
Address = expr.Compile()(x.Address)
};
}
public Expression<Func<Address, AddressDto>> AddressToDtoExpr() {
return x => New AddressDto() {
Id = x.Id,
City = x.City,
Country= x.Country
};
}
异常:The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
现在,我如果硬编码并将其嵌套到一个 Expression 中,然后将其放入 Linq-to-Entity 中,那么它就可以工作了:
//hardcode combined together into 1 expression with nested object
public static Expression<Func<User, UserDto>> UserToDtoExpr() {
return x => new UserDto() {
Id = x.Id,
Name = x.Name,
Address = New AddressDto() {
Id = x.Address.Id,
City= x.Address.City,
Country = x.Address.Country
}
};
}
但我不想像第二种方式那样对其进行硬编码,因为我想模块化和重用这些Expression 函数。如何修复第一种使其工作的方法?谢谢。
【问题讨论】:
标签: c# entity-framework linq-to-entities