仅支持初始值设定项、实体成员和实体导航属性 [英] Only initializers, entity members, and entity navigation properties are supported

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

问题描述

我收到此异常:

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

Payments 是一个包含字段金额的相关表,如果我删除显示有关付款的正确信息的 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?

按照以下建议的答案解决:

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

        return View(debts);
    }

推荐答案

Entity 正在尝试将您的 Paid 属性转换为 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天全站免登陆