c# - Checking for null returned items -
currently returning list of items have date range. of appointments have expired , others have not happened yet. displaying appointments have not expired yet. want check if appoinments have expired show first item.
var curapt = myappts.where(d => d.appt.endtime > datetime.utcnow).first(); if myappts.where(d => d.appt.endtime > datetime.utcnow) == null
var curapt = myappts.first(); how can structure consider both cases?
firstordefault , null coalescing operator (??):
var curapt = myappts.firstordefault(d => d.appt.endtime > datetime.utcnow) ?? myappts.first(); however, make less exception prone if have default value show if there no appointments @ all, chain null coalescing operator that:
var curapt = myappts.firstordefault(d => d.appt.endtime > datetime.utcnow) ?? myappts.firstordefault() ?? yourdefault;
Comments
Post a Comment