Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);     } 
like image 831
Marc Avatar asked Aug 02 '11 22:08

Marc


1 Answers

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.

like image 61
Eugene S. Avatar answered Sep 19 '22 13:09

Eugene S.