Reputation: 1021
I need to select data from database depending on Session value , but I'm getting this error always 'operator '==' cannot be applied to operands of type 'int?' and 'object' MVC '
I tried the following code in orders controller and orders view:
public ActionResult ordersCash()
{
return View(db.Lab_orders_Cash.Where(x => x.patient_no == Session["UserpatientNo"]).ToList());
}
I tried the solutions in this site by using (int) but its not working :
public ActionResult ordersCash()
{
return View(db.Lab_orders_Cash.Where(x => x.patient_no == (int)Session["UserpatientNo"]).ToList());
}
When i used (int)Session["UserpatientNo"])
i got this error :
LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.
Without where clause i can get all data but i need to filter and search by session value. How can i solve this error?
Upvotes: 0
Views: 820
Reputation: 152491
Pull the session variable into a variable:
public ActionResult ordersCash()
{
int userPatientNum = (int)Session["UserpatientNo"];
return View(db.Lab_orders_Cash.Where(x => x.patient_no == userPatientNum).ToList());
}
Upvotes: 1