How to handle null in Entity Framework Core using FirstAsync?

c# entity-framework-core

Question

  public async Task<ParagemRegisto> GetParagemRegistoOnGoingAsync(int registoId)
  {
        return await = _context.ParagensRegistos
            .Where(pr => pr.RegistoId == registoId && pr.HoraFim == null)
            .FirstAsync();
  }

This causes a null reference exception when there's no records found. How can I make it so it returns null if that is the case?

1
0
6/24/2019 8:51:37 PM

Popular Answer

You can use FirstOrDefaultAsync.

3
6/24/2019 7:51:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow