why I got this error Sequence contains no elements?

ziad abuqasem 1 Reputation point
2021-10-19T03:31:29.907+00:00

I am using the following code in the view and tried a lot of solutions but still get this error
Sequence Contains no elements , this is the view code :

    @model AljawdahNewSite.Models.Orders_Tables  
    @{  
        ViewBag.Title = "Result Details";  
        //Layout = "~/Views/Shared/_LayoutPatients.cshtml";  
        var hema = new List<int>() { 1 };  
        var bio = new List<int>() { 2 };  
        var ser = new List<int>() { 3 };  
        var hor = new List<int>() { 4 };  
        var histo = new List<int>() { 5 };  
        var culture = new List<int>() { 8 };  
        var para = new List<int>() { 6 };  
        var covid = new List<int>() { 9 };  
      
        var labPara = Model.LabParaResult.FirstOrDefault();  
        var labCult = Model.LabCultureResults.FirstOrDefault();  
        var labMicro = Model.LabMicroResults.FirstOrDefault();  
        var labHisto = Model.LabHistoResult.FirstOrDefault();  
        var LabResults = Model.LabResults;  
        var labhema = LabResults.FirstOrDefault(x => x.deptid == 1);  
        var labBio = LabResults.FirstOrDefault(x => x.deptid == 2);  
        var labSer = LabResults.FirstOrDefault(x => x.deptid == 3);  
        var labHor = LabResults.FirstOrDefault(x => x.deptid == 4);  
        var labcovid = LabResults.FirstOrDefault(x => x.deptid == 9);  
      
        var LABCLINICVIEW = Model.labClinicsViewResult;  
        var LABCASHVIEW = Model.labCashView;  
        var labPara1 = Model.LabParasitologyView.FirstOrDefault();  
        var labCult1 = Model.LabCulturesView.FirstOrDefault();  
        var labMicro1 = Model.LabMicroView.FirstOrDefault();  
        var labHisto1 = Model.LabHistopathologyView.FirstOrDefault();  
        var labhema1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 1);  
        var labBio1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 2);  
        var labSer1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 3);  
        var labHor1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 4);  
        var labcovid1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 9);  
    }   

this is part of controller code :

public ActionResult MasterDetails(int id)  
        {  
  
                
            var tables = new Orders_Tables  
            {  
                testsRanges = db.TestsRanges.ToList(),  
  
                LabResults = db.LAB_RESULTS.Where(o => o.ORDER_ID == id)  
                             .Include(p => p.LabTests)  
                             .Include(t => t.Patients).ToList(),  
  
  
                LabParaResult = db.LAB_PARA_RESULTS.Where(o => o.ORDER_ID == id).Include(t => t.Patients).Include(t => t.LabTests).Include(c => c.Customers).ToList(),  
                LabCultureResults = db.LAB_CULTURE_RESULT.Where(o => o.ORDER_ID == id).Include(t => t.Patients).Include(t => t.LabTests).Include(c => c.Customers).ToList(),  
                LabMicroResults = db.LAB_MICRO_NEGATIVE_RESULT.Where(o => o.ORDER_ID == id).ToList(),  
                labCashView = db.LAB_RESULT_CASH_VIEW.Where(o => o.order_number == id).ToList(),  
                labClinicsViewResult = db.LAB_RESULTS_CLINIC_VIEW.Where(o => o.order_number == id).ToList(),  
                LabParasitologyView = db.LAB_PARASITOLOGY_VIEW.Where(o => o.order_number == id).ToList(),  
                  
            };  
  
            return View(tables);  
             
        }  

the error show when try to run the result for

var labPara1 = Model.LabParasitologyView.FirstOrDefault();   

but the error appear in this line

var labcovid1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 9);  

141450-sequence-contains-error.png

I need your help please :

How I will handle the controller code

If labClinicsViewResult is empty and returned no value skip and go to the next

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2021-10-19T08:39:45.98+00:00

    Hi @ziad abuqasem ,
    The reason for the error is: you want to fetch data from a null.
    Maybe you can use DefaultIfEmpty(), if the given collection for calling DefaultIfEmpty() is empty, then the DefaultIfEmpty() method returns a new collection with a default value.

    labClinicsViewResult = db.LAB_RESULTS_CLINIC_VIEW.Where(o => o.order_number == id).DefaultIfEmpty().ToList();  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.