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); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With