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