I have the following query:
var maxNumber = dbContext.Where(a => a.Id == 9).Max(a => a.Sample_Num);
If there is no Id of 9, I get an error. I like to default the result to 0 if there is no Id of 9.
I tried:
var maxNumber = dbContext.Where(a => a.Id == 9).Max(a => a.Sample_Num) ?? 0;
as well as other variations but was not able to get it to work
You could use
Anyto check if there’s a matching element:or you could use
DefaultIfEmpty(defaultValue):