只支持初始化器,实体成员和实体导航属性 [英] Only initializers, entity members, and entity navigation properties are supported

查看:75
本文介绍了只支持初始化器,实体成员和实体导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个例外:


LINQ to Entities不支持指定的类型成员付费。
仅支持初始化,实体成员和实体导航属性

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);
        }
    }

付款是一个包含字段金额的相关表,查询工作,如果我删除Where子句显示有关付款的正确信息,任何线索代码有什么问题?

Payments is a Related table containing the field amount, The query works if I remove the Where clause showing correct information about the payments, any clue what's wrong with the code?

解决了像以下建议的答案: strong>

Solved like the answer suggested with :

    public ActionResult Index()
    {
        var debts = storeDB.Orders
            .OrderByDescending(o => o.DateCreated)
            .ToList()
            .Where(o => o.Paid == false);

        return View(debts);
    }


推荐答案

实体正在尝试转换您的付费属性到SQL,不能因为它不是表模式的一部分。

Entity is trying to convert your Paid property to SQL and can't because it's not part of the table schema.

您可以做的是让实体查询没有付费过滤器的表格,然后过滤掉未付费的。

What you can do is let Entity query the table with no Paid filter and then filter out the not Paid ones.

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);
}

当然,这意味着您将所有数据恢复到Web服务器并对其上的数据进行过滤。如果要在数据库服务器上进行过滤,可以在表上创建计算列或使用存储过程。

That, of course, would mean that you bringing all of the data back to the web server and filtering the data on it. If you want to filter on the DB server, you can create a Calculated Column on the table or use a Stored Procedure.

这篇关于只支持初始化器,实体成员和实体导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆