title: Entity Framework - Handle null value in Linq
body: <p>I'm writing two LINQ queries where I use my first query's result set in my second query.</p>
  
  <p>But in some cases when there is no data in the database table my first query returns null,
  and because of this my second query fails since <code>wsdetails.location</code> and <code>wsdetails.worklocation</code> are <code>null</code> causing an exception.</p>
  
  <p>Exception:</p>
  
  <blockquote>
    <p><em>Object reference not set to an instance of an object</em></p>
  </blockquote>
  
  <p>My code is this:</p>
  
  <pre><code>        var wsdetails = (from assetTable in Repository.Asset
                           join userAsset in Repository.UserAsset on
                           assetTable.Asset_Id equals userAsset.Asset_Id
                           join subLocationTable in Repository.SubLocation on
                           assetTable.Sub_Location_Id equals subLocationTable.Sub_Location_ID
                           where userAsset.User_Id == userCode
                           &amp;&amp; assetTable.Asset_TypeId == 1 &amp;&amp; assetTable.Asset_SubType_Id == 1
                           select new { workstation = subLocationTable.Sub_Location_Name, location = assetTable.Location_Id }).FirstOrDefault();
  
  
              result = (from emp in this.Repository.Employee
                        join designation in this.Repository.Designation on
                        emp.DesignationId equals designation.Id
                        where emp.Code == userCode
                        select new EmployeeDetails
                        {                             
                            firstname = emp.FirstName,
                            lastname = emp.LastName,                              
                            designation = designation.Title,
                            LocationId = wsdetails.location,
                            WorkStationName = wsdetails.workstation
                        }).SingleOrDefault();
  </code></pre>
  
  <p>As a workaround I can check </p>
  
  <pre><code>if wsdetails == null
  </code></pre>
  
  <p>and change my second LINQ logic, but I believe there are some ways to handle <code>null</code> values in LINQ itself like the <code>??</code> operator. </p>
  
  <p>But I tried this and it didn't work for me.</p>
  
  <p>Any help?</p>