why I got this error Sequence contains no elements

Viewed 285

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);
    } 


 
<div id="coviddiv">
        @if (labcovid1 != null && labcovid1.Report_Date.HasValue)
        {


            <table class="table table-bordered">
                <tr>
                    <td style="text-align:center">
                        <input type="submit" value="Covid-19 Result " @*onclick="PrintElemser('#serdiv')"*@ class="btn btn-primary" />
                        <a href="/RPT/WebForm1.aspx/?order_number=@Model.labClinicsViewResult.First().order_number&deptid=9&testid=@Model.labClinicsViewResult.First().Test_Id&Culture=@Model.labClinicsViewResult.First().Culture" class="btn btn-danger">Download Covid Result</a>
                    </td>
                </tr>
            </table>
            <div>
                <dl class="horizontal" style="border:solid;padding:10px">
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().Patient_Name)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().Patient_Name)</dd>
                    <dt style="width: 22%;display: inline-block;color:blue;margin-left:-20px">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().Customer_Name)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().Customer_Name)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().Patient_No)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().Patient_No)</dd>
                    <dt style="width: 22%;display: inline-block;margin-left:0px;color:blue;margin-left:-20px">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().Collection_Date)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().Collection_Date)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().SEX)</dt>
                    <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().SEX)</dd>
                    <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().Receiving_Date)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().Receiving_Date)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().AGE)</dt>
                    <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().AGE)</dd>
                    <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().Report_Date)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().Report_Date)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labClinicsViewResult.FirstOrDefault().order_number)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.labClinicsViewResult.FirstOrDefault().order_number)</dd>
                </dl>
            </div>
            <div class="tab-pane container active" id="home">
                <h5 class="text-uppercase p-2 text-center">Molecular Unit</h5>
                <table class="table table-bordered" cellpadding="5" cellspacing="5" border="1">
                    <thead>
                        <tr>
                            @*<th>@Html.DisplayNameFor(m => m.labCashView.FirstOrDefault().Patient_No)</th>
                                <th>@Html.DisplayNameFor(m => m.labCashView.FirstOrDefault().Patient_Name)</th>*@
                            <th>@Html.DisplayNameFor(m => m.labClinicsViewResult.FirstOrDefault().Test_Name)</th>
                            <th>@Html.DisplayNameFor(m => m.labClinicsViewResult.FirstOrDefault().Result)</th>
                            <th>@Html.DisplayNameFor(m => m.labClinicsViewResult.FirstOrDefault().Low_Range)</th>
                            <th>@Html.DisplayNameFor(m => m.labClinicsViewResult.FirstOrDefault().High_Range)</th>
                            <th>@Html.DisplayNameFor(m => m.labClinicsViewResult.FirstOrDefault().Text_Range)</th>
                            <th>@Html.DisplayNameFor(m => m.labClinicsViewResult.FirstOrDefault().Report_Date)</th>

                        </tr>
                    </thead>
                    @foreach (var employee in Model.labClinicsViewResult.Where(x => covid.Contains(x.DEPTID.GetValueOrDefault())))
                {
                        <tr>
                            @*<td>@employee.Patient_No</td>
                                <td>@employee.Patient_Name</td>*@
                            <td>@employee.Test_Name</td>
                            <td>@employee.Result</td>
                            <td>@employee.Low_Range</td>
                            <td>@employee.High_Range</td>
                            <td>@employee.Text_Range</td>
                            <td>@employee.Report_Date</td>

                        </tr>
                    }
                </table>
            </div>

        }

    </div>

<div id="paradiv">
        @if (labPara1 != null && labPara1.Report_Date.HasValue)
        {
          
            
            <table class="table table-bordered">
                <tr>
                    <td style="text-align:center">
                        <h3>  <input type="submit" style="text-align:center" value="Parasitology Result " onclick="PrintElempar('#paradiv')" class="btn btn-primary" /></h3>
                        <a href="/RPT/WebForm1.aspx/?order_number=@Model.LabParaResult.First().ORDER_ID&deptid=@Model.LabParaResult.First().deptid&testid=@Model.LabParaResult.First().TESTID&Culture=2" class="btn btn-danger">Print Parasitology Result</a>
                    </td>
                </tr>
            </table>
            <div>
                <dl class="horizontal" style="border:solid;padding:10px">
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Patient_Name)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Patient_Name)</dd>
                    <dt style="width: 22%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Customer_Name)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Customer_Name)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Patient_No)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Patient_No)</dd>
                    <dt style="width: 22%;display: inline-block;margin-left:0px;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Collection_Date)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Collection_Date)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().SEX)</dt>
                    <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().SEX)</dd>
                    <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Receiving_Date)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Receiving_Date)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().AGE)</dt>
                    <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().AGE)</dd>
                    <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Report_Date)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Report_Date)</dd>
                    <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().order_number)</dt>
                    <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().order_number)</dd>
                    @*<dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.LabParasitologyView.FirstOrDefault().Test_Name)</dt>
                        <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:25px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Test_Name)</dd>*@
                </dl>
            </div>
            <div class="tab-pane container active" id="home">
                @*<h5 class="text-uppercase p-2 text-center">Parasitology Department</h5>*@
                <div>
                    @*<dl class="horizontal" style="border:solid">
                            <dd style="width: 40%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.LabParasitologyView.FirstOrDefault().Test_Name)</dd>
                        </dl>*@


                    <table class="table table-bordered" cellpadding="5" cellspacing="5" border="1">
                        <thead>
                            <tr>
                                <th>@Html.DisplayNameFor(m => m.labtests.FirstOrDefault().TestName)</th>
                                <th>@Html.DisplayNameFor(m => m.LabParasitologyView.FirstOrDefault().SERIAL)</th>
                                <th>@Html.DisplayNameFor(m => m.LabParasitologyView.FirstOrDefault().TEXT)</th>
                                <th>@Html.DisplayNameFor(m => m.LabParasitologyView.FirstOrDefault().RESULT)</th>
                                <th>@Html.DisplayNameFor(m => m.LabParasitologyView.FirstOrDefault().UNIT)</th>
                                <th>@Html.DisplayNameFor(m => m.LabParasitologyView.FirstOrDefault().Reference_Range)</th>
                                <th>@Html.DisplayNameFor(m => m.LabParasitologyView.FirstOrDefault().Report_Date)</th>



                            </tr>
                        </thead>
                        @foreach (var employee in Model.LabParaResult.Where(x => para.Contains(x.deptid.GetValueOrDefault())))
                {
                            <tr>

                                <td>@employee.LabTests.TestName</td>
                                <td>@employee.SERIAL</td>
                                <td>@employee.TEXT</td>
                                <td>@employee.VALUE</td>
                                <td>@employee.unit</td>
                                <td>@employee.reference_range</td>
                                <td>@employee.APPROVED_DATE</td>



                            </tr>
                        }
                    </table>
                </div>
            </div>
        }
    </div>

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);

enter image description here

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 ?

THIS IS THE CLASS :

public class Orders_Tables
{
   // internal IQueryable<object> result { get; set; }
    public Lab_Orders  LabOrders { get; set; }
    public Lab_orders_Cash LabOrdersCash { get; set; }
    public Lab_Sample_status LabOrderStatus { get; set; }

    public IEnumerable<LAB_RESULTS> LabResults { get; set; }

    public IEnumerable<TestsRanges> testsRanges { get; set; }

    public IEnumerable<TestsUnits>  testUnits { get; set; }

    public IEnumerable<LAB_CULTURE_RESULT> LabCultureResults { get; set; }

    public IEnumerable<LAB_MICRO_NEGATIVE_RESULT> LabMicroResults { get; set; }
    public IEnumerable<LabTests> labtests { get; set; }
    public IEnumerable<LAB_RESULTS_CLINIC_VIEW> labClinicsViewResult { get; set; }
    public IEnumerable<LAB_RESULT_CASH_VIEW> labCashView { get; set; }

    public IEnumerable<LAB_PARASITOLOGY_VIEW> LabParasitologyView { get; set; }

    public IEnumerable<LAB_CULTURES_VIEW> LabCulturesView { get; set; }

    public IEnumerable<LAB_MICRO_VIEW> LabMicroView { get; set; }

    public IEnumerable<LAB_HISTOPATHOLOGY_VIEW> LabHistopathologyView { get; set; }

    public IEnumerable<LAB_HISTO_RESULT> LabHistoResult { get; set; }

    public IEnumerable<LAB_PARA_RESULTS> LabParaResult { get; set; }

    public Lab_Hematology_Samples LabSamples { get; set; }

  public Patients patients { get; set; }

    public IEnumerable<Customers> customers { get; set; }
}

THIS IS THE CLASS DEFINITIONS:

public partial class LAB_RESULTS_CLINIC_VIEW
    {
        public int order_number { get; set; }
        public int Test_Id { get; set; }
        public string Test_Name { get; set; }
        public Nullable<int> Result_Duration { get; set; }
        public Nullable<int> Culture { get; set; }
        public string Normal { get; set; }
        public Nullable<System.DateTime> Report_Date { get; set; }
        public string Result { get; set; }
        public string Notes { get; set; }
        public string Low_Range { get; set; }
        public string High_Range { get; set; }
        public string Panic { get; set; }
        public string Text_Range { get; set; }
        public string machine_name { get; set; }
        public Nullable<int> Customer_No { get; set; }
        public string Customer_Name { get; set; }
        public Nullable<int> Patient_No { get; set; }
        public string Patient_Name { get; set; }
        public string Clinic_File_No { get; set; }
        public string Category { get; set; }
        public Nullable<int> AGE { get; set; }
        public string SEX { get; set; }
        public string Test_Note { get; set; }
        public string UNIT { get; set; }
        public Nullable<int> DEPTID { get; set; }
        public Nullable<System.DateTime> Collection_Date { get; set; }
        public Nullable<System.DateTime> Receiving_Date { get; set; }
        public string Group_Name { get; set; }
        public Nullable<int> SERIAL { get; set; }
        public int GROUPID { get; set; }
        public Nullable<int> packageid { get; set; }
        public string EXAMINED_BY { get; set; }
        public string APPROVED_BY { get; set; }
        public string UPDATED_BY { get; set; }
        public Nullable<System.DateTime> UPDATED_DATE { get; set; }
        public string Comments { get; set; }
        public string department_name { get; set; }
    }

THIS IS THE VIEW SCRIPT IN THE DATABASE :

CREATE VIEW [dbo].[LAB_RESULTS_CLINIC_VIEW] AS 

SELECT  DISTINCT [LAB_RESULTS].ORDER_ID  as 'order number'
       ,labtests.TestId as 'Test Id'   
       ,labtests.TestName as 'Test Name'
       ,labtests.test_duration as 'Result Duration'
       ,labtests.culture as 'Culture'
       ,NORMAL_RESULT as 'Normal'     
       ,cast(LAB_RESULTS.APPROVED_DATE as datetime) as 'Report Date'
       ,CASE WHEN coalesce([RESULT_NUMBER],'') <> '' THEN RESULT_NUMBER WHEN coalesce(RESULT_REPORT, '') <> '' THEN RESULT_REPORT END AS Result 
       ,[RESULT_NOTE]   as 'Notes'
     ,TestsRanges.LowerLimit as 'Low Range'
      ,TestsRanges.UpperLimit as 'High Range'
     ,TestsRanges.panic_value as 'Panic'
      ,TestsRanges.text_range as 'Text Range'
       ,machines.machine_name
      ,LAB_RESULTS.CUSTID as 'Customer No'
      ,Customers.CustName as 'Customer Name'
      ,LAB_RESULTS.patient_no as 'Patient No'
      ,Patients.PATIENT_NAME as 'Patient Name'
      ,Patients.PCfileNo as 'Clinic File No'
      ,patientcat.CatName as 'Category'
      ,Patients.Age AS 'AGE'
      ,gender.gendername as 'SEX'
      ,labtests.TestNotes as 'Test Note'
      ,TestsUnits.UnitName as 'UNIT'
      ,LAB_RESULTS.deptid AS 'DEPTID'
      ,cast(Lab_Hematology_Samples.COLLECTION_DATE as datetime) as 'Collection Date'
      ,cast(Lab_Hematology_Samples.RECEIVING_DATE as datetime) as 'Receiving Date'
      ,Groups.groupdesc as 'Group Name'
      ,LAB_RESULTS.SERIAL
      ,LAB_RESULTS.GROUPID
      ,LAB_RESULTS.packageid
      ,LAB_RESULTS.EXAMINED_BY
      ,LAB_RESULTS.APPROVED_BY
      ,UPDATED_BY
      ,UPDATED_DATE
      ,Lab_Result_Comments.comments as 'Comments'
      ,Departments.Dept_name as 'department name'
      ,[LAB_RESULTS].branch_id as 'branchid'
     
      
  FROM [dbo].[LAB_RESULTS]
  inner join LabTests on LabTests.testid=LAB_RESULTS.TESTID
  inner join TestsRanges on  LAB_RESULTS.TESTID=TestsRanges.TestId 
  inner join Departments on  LAB_RESULTS.deptid=Departments.Dept_id 
  inner join PatientCat on patientcat.CatId = TestsRanges.CatId
  inner join Customers on Customers.CustId=LAB_RESULTS.CUSTID
  inner join patients on Patients.Patient_No = LAB_RESULTS.patient_no
 inner join machines on lab_results.machine_id = machines.machine_id
  inner join gender on gender.genderid = Patients.Gender
  inner join TestsUnits on TestsUnits.UnitId = LabTests.UnitId
  inner join Lab_Orders on Lab_Orders.ORDER_ID = LAB_RESULTS.ORDER_ID
  left join Lab_Result_Comments on LAB_RESULTS.ORDER_ID = Lab_Result_Comments.ORDER_ID
  inner join Lab_Hematology_Samples on Lab_Hematology_Samples.SAMPLE_ID = LAB_RESULTS.SAMPLE_ID
  inner join Groups on (Groups.groupid = LAB_RESULTS.GROUPID and Groups.Testid = LAB_RESULTS.TESTID )

  where (Lab_Orders.catid = TestsRanges.CatId or PatientCat.CatId =5)
  and  LAB_RESULTS.EXAMINED_DATE is not null 
  and  LAB_RESULTS.APPROVED_DATE is not null
  and  LAB_RESULTS.SAMPLE_STATUS=6
 and  (Patients.Gender = TestsRanges.PatientSex or TestsRanges.PatientSex = 1 )
  AND TestsRanges.machine_id = LAB_RESULTS.machine_id
 -- and LAB_RESULTS.update_count in (select max(update_count) from LAB_RESULTS where SAMPLE_STATUS = 6 and deptid = @deptid and ORDER_ID = @ORDER_ID)
 AND LAB_RESULTS.DEPTID IN (1,2,3,4,9)
  
  UNION ALL 
 
 SELECT DISTINCT [LAB_RESULTS].ORDER_ID  as 'order number'
       ,labtests.TestId as 'Test Id'     
       ,labtests.TestName as 'Test Name'
       ,labtests.test_duration as 'Result Duration'
       ,labtests.culture as 'Culture'
       ,NORMAL_RESULT as 'Normal'     
       ,cast(APPROVED_DATE as datetime) as 'Report Date'
       ,CASE WHEN coalesce([RESULT_NUMBER],'') <> '' THEN RESULT_NUMBER WHEN coalesce(RESULT_REPORT, '') <> '' THEN RESULT_REPORT END AS Result 
       ,[RESULT_NOTE]   as 'Notes'
      ,TestsRanges.LowerLimit as 'Low Range'
      ,TestsRanges.UpperLimit as 'High Range'
      ,TestsRanges.panic_value as 'Panic'
      ,TestsRanges.text_range as 'Text Range'
       ,machines.machine_name
      ,LAB_RESULTS.CUSTID as 'Customer No'
      ,Customers.CustName as 'Customer Name'
      ,LAB_RESULTS.patient_no as 'Patient No'
      ,Patients.PATIENT_NAME as 'Patient Name'
      ,Patients.PCfileNo as 'Clinic File No'
      ,patientcat.CatName as 'Category'
      ,Patients.Age AS 'AGE'
      ,gender.gendername as 'SEX'
      ,labtests.TestNotes as 'Test Note'
      ,TestsUnits.UnitName as 'UNIT'
      ,LAB_RESULTS.deptid AS 'DEPTID'
      ,cast(Lab_Hematology_Samples.COLLECTION_DATE as datetime) as 'Collection Date'
      ,cast(Lab_Hematology_Samples.RECEIVING_DATE as datetime)as 'Receiving Date'
      ,NULL AS 'Group Name'
      ,LAB_RESULTS.SERIAL
      ,LAB_RESULTS.GROUPID
      ,LAB_RESULTS.packageid
      ,LAB_RESULTS.EXAMINED_BY
      ,LAB_RESULTS.APPROVED_BY
      ,UPDATED_BY
      ,UPDATED_DATE
      ,Lab_Result_Comments.comments as 'Comments'
      ,Departments.Dept_name as 'department name'
      ,[LAB_RESULTS].branch_id as 'branchid'
      
      
  FROM [dbo].[LAB_RESULTS]
  inner join LabTests on LabTests.testid=LAB_RESULTS.TESTID
   inner join TestsRanges on TestsRanges.TestId = LAB_RESULTS.TESTID
   inner join Departments on  LAB_RESULTS.deptid=Departments.Dept_id 
  inner join PatientCat on patientcat.CatId = TestsRanges.CatId
  inner join Customers on Customers.CustId=LAB_RESULTS.CUSTID
  inner join machines on lab_results.machine_id = machines.machine_id
  inner join patients on Patients.Patient_No = LAB_RESULTS.patient_no
  inner join gender on gender.genderid = Patients.Gender
  inner join TestsUnits on TestsUnits.UnitId = LabTests.UnitId
  left join Lab_Result_Comments on LAB_RESULTS.ORDER_ID = Lab_Result_Comments.ORDER_ID
  inner join Lab_Orders on Lab_Orders.ORDER_ID = LAB_RESULTS.ORDER_ID
  inner join Lab_Hematology_Samples on Lab_Hematology_Samples.SAMPLE_ID = LAB_RESULTS.SAMPLE_ID
  

  where (Lab_Orders.catid = TestsRanges.CatId or PatientCat.CatId =5)
  and  LAB_RESULTS.EXAMINED_DATE is not null 
  and  LAB_RESULTS.APPROVED_DATE is not null
  and  LAB_RESULTS.SAMPLE_STATUS=6
  and  (Patients.Gender = TestsRanges.PatientSex or TestsRanges.PatientSex = 1 )
  and  LAB_RESULTS.GROUPID = 0
 -- and LAB_RESULTS.update_count in (select max(update_count) from LAB_RESULTS where SAMPLE_STATUS = 6 and deptid = @deptid and ORDER_ID = @ORDER_ID)
 -- and LAB_RESULTS.deptid = @deptid
  AND TestsRanges.machine_id = LAB_RESULTS.machine_id
  AND LAB_RESULTS.DEPTID IN (1,2,3,4,9)

Explain more details about the issue :

The doctor login to the site and open the result by click result button ,

if the result in departments (1,2,3,4,5,9) the controller masterdetails will show the result in next page

but if the result in department (6,8) which is belongs to the table (LAB_PARA_RESULTS or LAB_CULTURE_RESULT )

its not open and show the error this is the image before the error :

enter image description here

1 Answers

It looks like the sequence that contains no elements is LABCLINICVIEW. Which is being assigned from your model variable Orders_Tables.labClinicsViewResult. You are assigning this variable with the database call db.LAB_RESULTS_CLINIC_VIEW.Where(o => o.order_number == id).ToList(), so it seems to me that this database call is returning an empty set. Try debugging and step through and stop on the line return View(tables); and then inspect your variable tables to see if the member variable tables.labClinicsViewResult does in fact have anything in it. If it is empty then that's your problem. If it isn't empty then your problem is likely in the call var labcovid1 = LABCLINICVIEW.FirstOrDefault(x => x.DEPTID == 9); and it might not have a value where the DEPTID is equal to 9.

Related