C Sharp - Linq - FirstOrDefault

.FirstOrDefault

This command is similar to the SingleOrDefault function, but when no records are found, it returns a null.

  • Considered faster than .SingleOrDefault
  • Similar commands: First(), .Take(1)
  • This is similar to a TOP 1

Sample

1
2
3
var dbPrograms = _context.Programs.FirstOrDefault(e => e.CategoryId == id && e.IsDeleted == false);

if (dbProgram.Count == 0 && dbPermit.Count == 0)

from AFShin - instead of count, use .Any()

References