title: Cannot assign Nullable<Int64> to Linq.Table.Max()?
body: <p>I have the following two lines of code in my program. The goal is to return the highest number from the Value field. </p>
  
  <pre><code>MyDataContext context = new MyDataContext();
  long? id = (long?)context.MyTable.Max(x =&gt; x.Value);
  </code></pre>
  
  <p>The Value field is a bigint field in the table. The table also cannot guarantee that it contains rows, in fact that it is scenario I am testing now. I am attempting to run this when MyTable has no rows.</p>
  
  <p>The second line returns an invalid operation exception.</p>
  
  <blockquote>
    <p>The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type.</p>
  </blockquote>
  
  <p>I've tried it several different ways but I am unable to get this to work. I obviously not understanding something important about nullable types in C#.</p>
  
  <p>EDIT (solution):</p>
  
  <p>Thanks to Chris's answer I was able to find the actual cause of my problem and solution. It turns out that it wasn't so much that the Value field was null but that since x inside the lambda expression was a null row. So I added a check inside the lambda to see if x was null, if not then I can safely check if Value is null.</p>
  
  <pre><code>MyDataContext context = new MyDataContext();
  long? id = context.MyTable.Max(x =&gt; x == null ? (long?)0 : (long?)x.Value);
  </code></pre>