title: why I got this error Sequence contains no elements
body: <p>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 :</p>
  <pre><code>    @model AljawdahNewSite.Models.Orders_Tables
      @{
          ViewBag.Title = &quot;Result Details&quot;;
          //Layout = &quot;~/Views/Shared/_LayoutPatients.cshtml&quot;;
          var hema = new List&lt;int&gt;() { 1 };
          var bio = new List&lt;int&gt;() { 2 };
          var ser = new List&lt;int&gt;() { 3 };
          var hor = new List&lt;int&gt;() { 4 };
          var histo = new List&lt;int&gt;() { 5 };
          var culture = new List&lt;int&gt;() { 8 };
          var para = new List&lt;int&gt;() { 6 };
          var covid = new List&lt;int&gt;() { 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 =&gt; x.deptid == 1);
          var labBio = LabResults.FirstOrDefault(x =&gt; x.deptid == 2);
          var labSer = LabResults.FirstOrDefault(x =&gt; x.deptid == 3);
          var labHor = LabResults.FirstOrDefault(x =&gt; x.deptid == 4);
          var labcovid = LabResults.FirstOrDefault(x =&gt; 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 =&gt; x.DEPTID == 1);
          var labBio1 = LABCLINICVIEW.FirstOrDefault(x =&gt; x.DEPTID == 2);
          var labSer1 = LABCLINICVIEW.FirstOrDefault(x =&gt; x.DEPTID == 3);
          var labHor1 = LABCLINICVIEW.FirstOrDefault(x =&gt; x.DEPTID == 4);
          var labcovid1 = LABCLINICVIEW.FirstOrDefault(x =&gt; x.DEPTID == 9);
      } 
  
  
   
  &lt;div id=&quot;coviddiv&quot;&gt;
          @if (labcovid1 != null &amp;&amp; labcovid1.Report_Date.HasValue)
          {
  
  
              &lt;table class=&quot;table table-bordered&quot;&gt;
                  &lt;tr&gt;
                      &lt;td style=&quot;text-align:center&quot;&gt;
                          &lt;input type=&quot;submit&quot; value=&quot;Covid-19 Result &quot; @*onclick=&quot;PrintElemser('#serdiv')&quot;*@ class=&quot;btn btn-primary&quot; /&gt;
                          &lt;a href=&quot;/RPT/WebForm1.aspx/?order_number=@Model.labClinicsViewResult.First().order_number&amp;deptid=9&amp;testid=@Model.labClinicsViewResult.First().Test_Id&amp;Culture=@Model.labClinicsViewResult.First().Culture&quot; class=&quot;btn btn-danger&quot;&gt;Download Covid Result&lt;/a&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;div&gt;
                  &lt;dl class=&quot;horizontal&quot; style=&quot;border:solid;padding:10px&quot;&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Patient_Name)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Patient_Name)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;color:blue;margin-left:-20px&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Customer_Name)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Customer_Name)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Patient_No)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Patient_No)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;margin-left:0px;color:blue;margin-left:-20px&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Collection_Date)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Collection_Date)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().SEX)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 20%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().SEX)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;margin-left:60px;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Receiving_Date)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Receiving_Date)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().AGE)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 20%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().AGE)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;margin-left:60px;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Report_Date)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().Report_Date)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().order_number)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.labClinicsViewResult.FirstOrDefault().order_number)&lt;/dd&gt;
                  &lt;/dl&gt;
              &lt;/div&gt;
              &lt;div class=&quot;tab-pane container active&quot; id=&quot;home&quot;&gt;
                  &lt;h5 class=&quot;text-uppercase p-2 text-center&quot;&gt;Molecular Unit&lt;/h5&gt;
                  &lt;table class=&quot;table table-bordered&quot; cellpadding=&quot;5&quot; cellspacing=&quot;5&quot; border=&quot;1&quot;&gt;
                      &lt;thead&gt;
                          &lt;tr&gt;
                              @*&lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labCashView.FirstOrDefault().Patient_No)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labCashView.FirstOrDefault().Patient_Name)&lt;/th&gt;*@
                              &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labClinicsViewResult.FirstOrDefault().Test_Name)&lt;/th&gt;
                              &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labClinicsViewResult.FirstOrDefault().Result)&lt;/th&gt;
                              &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labClinicsViewResult.FirstOrDefault().Low_Range)&lt;/th&gt;
                              &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labClinicsViewResult.FirstOrDefault().High_Range)&lt;/th&gt;
                              &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labClinicsViewResult.FirstOrDefault().Text_Range)&lt;/th&gt;
                              &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labClinicsViewResult.FirstOrDefault().Report_Date)&lt;/th&gt;
  
                          &lt;/tr&gt;
                      &lt;/thead&gt;
                      @foreach (var employee in Model.labClinicsViewResult.Where(x =&gt; covid.Contains(x.DEPTID.GetValueOrDefault())))
                  {
                          &lt;tr&gt;
                              @*&lt;td&gt;@employee.Patient_No&lt;/td&gt;
                                  &lt;td&gt;@employee.Patient_Name&lt;/td&gt;*@
                              &lt;td&gt;@employee.Test_Name&lt;/td&gt;
                              &lt;td&gt;@employee.Result&lt;/td&gt;
                              &lt;td&gt;@employee.Low_Range&lt;/td&gt;
                              &lt;td&gt;@employee.High_Range&lt;/td&gt;
                              &lt;td&gt;@employee.Text_Range&lt;/td&gt;
                              &lt;td&gt;@employee.Report_Date&lt;/td&gt;
  
                          &lt;/tr&gt;
                      }
                  &lt;/table&gt;
              &lt;/div&gt;
  
          }
  
      &lt;/div&gt;
  
  &lt;div id=&quot;paradiv&quot;&gt;
          @if (labPara1 != null &amp;&amp; labPara1.Report_Date.HasValue)
          {
            
              
              &lt;table class=&quot;table table-bordered&quot;&gt;
                  &lt;tr&gt;
                      &lt;td style=&quot;text-align:center&quot;&gt;
                          &lt;h3&gt;  &lt;input type=&quot;submit&quot; style=&quot;text-align:center&quot; value=&quot;Parasitology Result &quot; onclick=&quot;PrintElempar('#paradiv')&quot; class=&quot;btn btn-primary&quot; /&gt;&lt;/h3&gt;
                          &lt;a href=&quot;/RPT/WebForm1.aspx/?order_number=@Model.LabParaResult.First().ORDER_ID&amp;deptid=@Model.LabParaResult.First().deptid&amp;testid=@Model.LabParaResult.First().TESTID&amp;Culture=2&quot; class=&quot;btn btn-danger&quot;&gt;Print Parasitology Result&lt;/a&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;div&gt;
                  &lt;dl class=&quot;horizontal&quot; style=&quot;border:solid;padding:10px&quot;&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Patient_Name)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Patient_Name)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Customer_Name)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Customer_Name)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Patient_No)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Patient_No)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;margin-left:0px;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Collection_Date)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Collection_Date)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().SEX)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 20%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().SEX)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;margin-left:60px;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Receiving_Date)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Receiving_Date)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().AGE)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 20%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().AGE)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 22%;display: inline-block;margin-left:60px;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Report_Date)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Report_Date)&lt;/dd&gt;
                      &lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().order_number)&lt;/dt&gt;
                      &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:-50px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().order_number)&lt;/dd&gt;
                      @*&lt;dt style=&quot;width: 20%;display: inline-block;color:blue;&quot;&gt;@Html.DisplayNameFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Test_Name)&lt;/dt&gt;
                          &lt;dd style=&quot;width: 25%;display: inline-block;margin: 0px;margin-left:25px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Test_Name)&lt;/dd&gt;*@
                  &lt;/dl&gt;
              &lt;/div&gt;
              &lt;div class=&quot;tab-pane container active&quot; id=&quot;home&quot;&gt;
                  @*&lt;h5 class=&quot;text-uppercase p-2 text-center&quot;&gt;Parasitology Department&lt;/h5&gt;*@
                  &lt;div&gt;
                      @*&lt;dl class=&quot;horizontal&quot; style=&quot;border:solid&quot;&gt;
                              &lt;dd style=&quot;width: 40%;display: inline-block;margin: 0px;margin-left:0px&quot;&gt;@Html.DisplayFor(model =&gt; model.LabParasitologyView.FirstOrDefault().Test_Name)&lt;/dd&gt;
                          &lt;/dl&gt;*@
  
  
                      &lt;table class=&quot;table table-bordered&quot; cellpadding=&quot;5&quot; cellspacing=&quot;5&quot; border=&quot;1&quot;&gt;
                          &lt;thead&gt;
                              &lt;tr&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.labtests.FirstOrDefault().TestName)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.LabParasitologyView.FirstOrDefault().SERIAL)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.LabParasitologyView.FirstOrDefault().TEXT)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.LabParasitologyView.FirstOrDefault().RESULT)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.LabParasitologyView.FirstOrDefault().UNIT)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.LabParasitologyView.FirstOrDefault().Reference_Range)&lt;/th&gt;
                                  &lt;th&gt;@Html.DisplayNameFor(m =&gt; m.LabParasitologyView.FirstOrDefault().Report_Date)&lt;/th&gt;
  
  
  
                              &lt;/tr&gt;
                          &lt;/thead&gt;
                          @foreach (var employee in Model.LabParaResult.Where(x =&gt; para.Contains(x.deptid.GetValueOrDefault())))
                  {
                              &lt;tr&gt;
  
                                  &lt;td&gt;@employee.LabTests.TestName&lt;/td&gt;
                                  &lt;td&gt;@employee.SERIAL&lt;/td&gt;
                                  &lt;td&gt;@employee.TEXT&lt;/td&gt;
                                  &lt;td&gt;@employee.VALUE&lt;/td&gt;
                                  &lt;td&gt;@employee.unit&lt;/td&gt;
                                  &lt;td&gt;@employee.reference_range&lt;/td&gt;
                                  &lt;td&gt;@employee.APPROVED_DATE&lt;/td&gt;
  
  
  
                              &lt;/tr&gt;
                          }
                      &lt;/table&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          }
      &lt;/div&gt;
  </code></pre>
  <p>this is part of controller code :</p>
  <pre><code>public ActionResult MasterDetails(int id)
          {
  
                
              var tables = new Orders_Tables
              {
                  testsRanges = db.TestsRanges.ToList(),
  
                  LabResults = db.LAB_RESULTS.Where(o =&gt; o.ORDER_ID == id)
                               .Include(p =&gt; p.LabTests)
                               .Include(t =&gt; t.Patients).ToList(),
  
  
                  LabParaResult = db.LAB_PARA_RESULTS.Where(o =&gt; o.ORDER_ID == id).Include(t =&gt; t.Patients).Include(t =&gt; t.LabTests).Include(c =&gt; c.Customers).ToList(),
                  LabCultureResults = db.LAB_CULTURE_RESULT.Where(o =&gt; o.ORDER_ID == id).Include(t =&gt; t.Patients).Include(t =&gt; t.LabTests).Include(c =&gt; c.Customers).ToList(),
                  LabMicroResults = db.LAB_MICRO_NEGATIVE_RESULT.Where(o =&gt; o.ORDER_ID == id).ToList(),
                  labCashView = db.LAB_RESULT_CASH_VIEW.Where(o =&gt; o.order_number == id).ToList(),
                  labClinicsViewResult = db.LAB_RESULTS_CLINIC_VIEW.Where(o =&gt; o.order_number == id).ToList(),
                  LabParasitologyView = db.LAB_PARASITOLOGY_VIEW.Where(o =&gt; o.order_number == id).ToList(),
                  
              };
  
              return View(tables);
             
          }
  </code></pre>
  <p>the error show when try to run the result for</p>
  <pre><code>var labPara1 = Model.LabParasitologyView.FirstOrDefault(); 
  </code></pre>
  <p>but the error appear in this line</p>
  <pre><code>var labcovid1 = LABCLINICVIEW.FirstOrDefault(x =&gt; x.DEPTID == 9);
  </code></pre>
  <p><a href="https://i.stack.imgur.com/6JPpa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6JPpa.png" alt="enter image description here" /></a></p>
  <p>I need your help please :</p>
  <p>How I will handle the controller code</p>
  <p>If <code>labClinicsViewResult</code>  is empty and returned no value skip and go to the next ?</p>
  <p>THIS IS THE CLASS :</p>
  <pre><code>public class Orders_Tables
  {
     // internal IQueryable&lt;object&gt; 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&lt;LAB_RESULTS&gt; LabResults { get; set; }
  
      public IEnumerable&lt;TestsRanges&gt; testsRanges { get; set; }
  
      public IEnumerable&lt;TestsUnits&gt;  testUnits { get; set; }
  
      public IEnumerable&lt;LAB_CULTURE_RESULT&gt; LabCultureResults { get; set; }
  
      public IEnumerable&lt;LAB_MICRO_NEGATIVE_RESULT&gt; LabMicroResults { get; set; }
      public IEnumerable&lt;LabTests&gt; labtests { get; set; }
      public IEnumerable&lt;LAB_RESULTS_CLINIC_VIEW&gt; labClinicsViewResult { get; set; }
      public IEnumerable&lt;LAB_RESULT_CASH_VIEW&gt; labCashView { get; set; }
  
      public IEnumerable&lt;LAB_PARASITOLOGY_VIEW&gt; LabParasitologyView { get; set; }
  
      public IEnumerable&lt;LAB_CULTURES_VIEW&gt; LabCulturesView { get; set; }
  
      public IEnumerable&lt;LAB_MICRO_VIEW&gt; LabMicroView { get; set; }
  
      public IEnumerable&lt;LAB_HISTOPATHOLOGY_VIEW&gt; LabHistopathologyView { get; set; }
  
      public IEnumerable&lt;LAB_HISTO_RESULT&gt; LabHistoResult { get; set; }
  
      public IEnumerable&lt;LAB_PARA_RESULTS&gt; LabParaResult { get; set; }
  
      public Lab_Hematology_Samples LabSamples { get; set; }
  
    public Patients patients { get; set; }
  
      public IEnumerable&lt;Customers&gt; customers { get; set; }
  }
  </code></pre>
  <p>THIS IS THE CLASS DEFINITIONS:</p>
  <pre><code>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&lt;int&gt; Result_Duration { get; set; }
          public Nullable&lt;int&gt; Culture { get; set; }
          public string Normal { get; set; }
          public Nullable&lt;System.DateTime&gt; 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&lt;int&gt; Customer_No { get; set; }
          public string Customer_Name { get; set; }
          public Nullable&lt;int&gt; Patient_No { get; set; }
          public string Patient_Name { get; set; }
          public string Clinic_File_No { get; set; }
          public string Category { get; set; }
          public Nullable&lt;int&gt; AGE { get; set; }
          public string SEX { get; set; }
          public string Test_Note { get; set; }
          public string UNIT { get; set; }
          public Nullable&lt;int&gt; DEPTID { get; set; }
          public Nullable&lt;System.DateTime&gt; Collection_Date { get; set; }
          public Nullable&lt;System.DateTime&gt; Receiving_Date { get; set; }
          public string Group_Name { get; set; }
          public Nullable&lt;int&gt; SERIAL { get; set; }
          public int GROUPID { get; set; }
          public Nullable&lt;int&gt; packageid { get; set; }
          public string EXAMINED_BY { get; set; }
          public string APPROVED_BY { get; set; }
          public string UPDATED_BY { get; set; }
          public Nullable&lt;System.DateTime&gt; UPDATED_DATE { get; set; }
          public string Comments { get; set; }
          public string department_name { get; set; }
      }
  </code></pre>
  <p>THIS IS THE VIEW SCRIPT IN THE DATABASE :</p>
  <pre><code>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],'') &lt;&gt; '' THEN RESULT_NUMBER WHEN coalesce(RESULT_REPORT, '') &lt;&gt; '' 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],'') &lt;&gt; '' THEN RESULT_NUMBER WHEN coalesce(RESULT_REPORT, '') &lt;&gt; '' 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)
  </code></pre>
  <p>Explain more details about the issue :</p>
  <p>The doctor login to the site and open the result by click result button ,</p>
  <p>if the result in departments (1,2,3,4,5,9) the controller masterdetails will show the result in next page</p>
  <p>but if the result in department (6,8) which is belongs to the table (LAB_PARA_RESULTS or LAB_CULTURE_RESULT )</p>
  <p>its not open and show the error this is the image before the error :</p>
  <p><a href="https://i.stack.imgur.com/XPLak.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XPLak.png" alt="enter image description here" /></a></p>