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

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -