I prefer DefaultIfEmpty () instead of .FirstOrDefault ()
and thus we can avoid if we check
eg
var obj = DBContext.MyTable.Where(x => x.ID == 2).DefaultIfEmpty().MyColumn;
or
var obj = DBContext.MyTable.Where(x => x.ID == 2).DefaultIfEmpty(string.Empty).MyColumn;
The concept of DefaultIfEmpty is simple: it replaces an empty collection with a set of one default value.
The default value for int is 0. Thus, DefaultIfEmpty in the list gives a list with one null element.
Hope this helps.
source share