0

I am trying to view the data and check the session parameter but I got error :

Additional information: 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.

This is the controller code :

public ActionResult Results()
        {
            var samples = _context.RESULTS.Where(s => s.status==2 && s.custid == (int)Session["HospitalId"]).ToList(); ;
            return View(samples);
        }

How to solve this error please and check the session parameter value when view the data ?

I changed the code :

int hospitalId = (int)Session["HospitalId"];
var samples = _context.RESULTS.Where(s => s.status == 2 && s.custid == hospitalId).ToList();

but got error :

 object reference not set to an instance of an object 

I send the hospitalId when click details button like this

@Html.ActionLink("Details","Index","Samples" , new { hospitalId = item.user_id},new {@class = "btn btn-primary" })

This is the controller code INDEX:

 public ActionResult Index()
        {
            int[] exclude = { 13, 14, 15 };
            var samples = _context.samples.Where(m => !exclude.Contains(m.id)).ToList();
            return View(samples);
            
        }

How can I save the hospitalid value from this link and use it in the second controller ?

int hospitalId = (int)Session["HospitalId"];
    var samples = _context.RESULTS.Where(s => s.status == 2 && s.custid == hospitalId).ToList();
MarwanAbu
  • 181
  • 8
  • 1
    don't use *Session["HospitalId"]* in your Linq query assign into a variable then use the variable in your lambda. hope that will work – coder_b Jun 25 '22 at 20:29
  • @coder_b I changed it but it seems not saved correct how to save hospitalId from actionlink @Html.ActionLink("Details","Index","Samples" , new { hospitalId = item.user_id} and use it in Linq query ? – MarwanAbu Jun 25 '22 at 21:08
  • how to receive parameters from the action link https://stackoverflow.com/questions/5593759/actionlink-with-multiple-parameters – coder_b Jun 25 '22 at 21:15

1 Answers1

1

The Linq must be translatable to SQL, as it is what happens, EF translates it to SQL and then runs it. (int)Session["HospitalId"] seems to confuse EF. do it like this instead:

int hospitalId = (int)Session["HospitalId"];
var samples = _context.RESULTS.Where(s => s.status == 2 && s.custid == hospitalId).ToList();
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • I tried it but got another error now Additional information: Object reference not set to an instance of an object. , I got Session["HospitalId"] from another controller , I updated the question please check updates – MarwanAbu Jun 25 '22 at 20:35
  • Well, I cannot say anything about this, it seems that the session is null there, which has nothing to do with the Linq query. you must find out why the session is null. – Ashkan Mobayen Khiabani Jun 25 '22 at 20:37
  • Thank you so much I updated the code and how can I save hospitalId value view actionlink @Html.ActionLink("Details","Index","Samples" , new { hospitalId = item.user_id} and use it in where statement ? – MarwanAbu Jun 25 '22 at 20:58