title: .Net Core 2.1 EF Core Null Reference Exception when searching database with Where linq query
body: <p>I have a pretty simple query here:</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Index(string sortOrder)
      {
          ApplicationUser user = await _userManager.GetUserAsync(User);
          var context = _context.Engine
              .Include(e =&gt; e.EngineType)
              .Where(e =&gt; e.Company == user.Company);
  
          return View(await context.ToListAsync());
      }
  </code></pre>
  
  <p>The problem is it gives me a NullReferenceException when it tries to return. I know the problem is the Where statement because if I use the below it works fine.</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Index(string sortOrder)
      {
          ApplicationUser user = await _userManager.GetUserAsync(User);
          var context = _context.Engine
              .Include(e =&gt; e.EngineType);
  
          return View(await context.ToListAsync());
      }
  </code></pre>
  
  <p>I've shifted things around to no avail.
  I found this similiar issue on StackOverflow: <a href="https://stackoverflow.com/questions/50994289/ef-core-2-1-props-inside-where-cause-null-reference">EF Core 2.1 Props inside where cause null reference</a> but ToList() doesn't work for me either.</p>
  
  <p>Anyone know what's going on?</p>
  
  <p>EDIT</p>
  
  <p>The Error:</p>
  
  <pre><code>NullReferenceException: Object reference not set to an instance of an object.
  
  lambda_method(Closure , Company )
  Microsoft.EntityFrameworkCore.Metadata.Internal.ClrPropertyGetter&lt;TEntity, TValue&gt;.GetClrValue(object instance)
  Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedPropertyRelationalParameter.AddDbParameter(DbCommand command, object value)
  Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary&lt;string, object&gt; parameterValues)
  Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.CreateCommand(IRelationalConnection connection, IReadOnlyDictionary&lt;string, object&gt; parameterValues)
  Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary&lt;string, object&gt; parameterValues, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable&lt;T&gt;+AsyncEnumerator.BufferlessMoveNext(DbContext _, bool buffer, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync&lt;TState, TResult&gt;(TState state, Func&lt;DbContext, TState, CancellationToken, Task&lt;TResult&gt;&gt; operation, Func&lt;DbContext, TState, CancellationToken, Task&lt;ExecutionResult&lt;TResult&gt;&gt;&gt; verifySucceeded, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable&lt;T&gt;+AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
  System.Linq.AsyncEnumerable+SelectEnumerableAsyncIterator&lt;TSource, TResult&gt;.MoveNextCore(CancellationToken cancellationToken)
  System.Linq.AsyncEnumerable+AsyncIterator&lt;TSource&gt;.MoveNext(CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor&lt;T&gt;+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
  System.Linq.AsyncEnumerable.Aggregate_&lt;TSource, TAccumulate, TResult&gt;(IAsyncEnumerable&lt;TSource&gt; source, TAccumulate seed, Func&lt;TAccumulate, TSource, TAccumulate&gt; accumulator, Func&lt;TAccumulate, TResult&gt; resultSelector, CancellationToken cancellationToken)
  InstantBSI.Controllers.EnginesController.Index(string sortOrder) in EnginesController.cs
              return View(await context.ToListAsync());
  </code></pre>
  
  <p>My Engine class:</p>
  
  <pre><code>    public int Id { get; set; }
      public int CompanyId { get; set; }
      public Company Company { get; set; }
      [Display(Name = "Engine Type")]
      public int EngineTypeId { get; set; }
      public EngineType EngineType { get; set; }
      public List&lt;Inspection&gt; Inspections { get; set; }
  
      [Display(Name = "Serial Number")]
      public long SerialNumber { get; set; }
      [Display(Name = "License Number")]
      [DisplayFormat(NullDisplayText = "N/A")]
      public long LicenseNumber { get; set; }
      [Display(Name = "Total Flight Cycles")]
      public int TotalFlightCycles { get; set; }
      [Display(Name = "Total Flight Hours")]
      public int TotalFlightHours { get; set; }
      [Required]
      [Display(Name = "Aircraft Maintenence Manual")]
      public string AircraftMaintenenceManual { get; set; }
  </code></pre>