I have a not nullable field (Num
)
class MyTable
{
//...
public int Num { get; set; }
public string Category { get; set; }
//...
}
want to find maximum Num
for Category == "A"
var maxnum = myTable
.Where(r => r.Category == "A")
.Max(r => r.Num);
the problem occurred when there wasn't any record of category == "A"
. Because the result of Where()
is null so the result of Max()
will be null but when Num
is not nullable the exception occurred.
I can fix it by setting Num
as nullable in table design but I don't like this solution while Num
should has value and shouldn't be nullable.
Any suggestion? Is there a way that I accept null value for Num while Num is not nullable? or any better query?