Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

linq - Only initializers, entity members, and entity navigation properties are supported

I'm getting this exception :

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

My Model class

public partial class Order
{
    public bool Paid {
        get {
            return TotalPaid >= Total;
        }
    }

    public decimal TotalPaid {
        get {
            return Payments.Sum(p => p.Amount);
        }
    }

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?

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);
    }
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...