我遇到了这个异常:
The specified type member 'Paid' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
public ActionResult Index()
{
var debts = storeDB.Orders
.Where(o => o.Paid == false)
.OrderByDescending(o => o.DateCreated);
return View(debts);
}
我的模型类
public partial class Order
{
public bool Paid {
get {
return TotalPaid >= Total;
}
}
public decimal TotalPaid {
get {
return Payments.Sum(p => p.Amount);
}
}
Payments 是一个包含字段金额的相关表,如果我删除显示有关付款的正确信息的Where 子句,查询就会工作,代码有什么问题吗?
像建议的答案一样解决:
public ActionResult Index()
{
var debts = storeDB.Orders
.OrderByDescending(o => o.DateCreated)
.ToList()
.Where(o => o.Paid == false);
return View(debts);
}
最佳答案
实体正在尝试将您的付费属性转换为 SQL,但不能,因为它不是表架构的一部分。
你可以做的是让Entity查询没有付费过滤器的表,然后过滤掉非付费的。
public ActionResult Index()
{
var debts = storeDB.Orders
//.Where(o => o.Paid == false)
.OrderByDescending(o => o.DateCreated);
debts = debts.Where(o => o.Paid == false);
return View(debts);
}
当然,这意味着您将所有数据带回网络服务器并过滤其中的数据。如果要在数据库服务器上进行过滤,可以在表上创建计算列或使用存储过程。
关于linq - 仅支持初始值设定项、实体成员和实体导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6919709/