Q:

compare none value by linq

I have an integer column(not null) in a sql server 2008 database/table, I want to retrieve it. 
  // go to the table and get the largest number, if none exists, use 0. 
        int iNumber = iContext. DetailsRecords. Max(x => x.Number); // from entity

But at the very beginning, the table is empty.  I want to set is as 0. 
How to change the code? 

A:

If you don't want to check if the DetailsRecords is null, but rather handle if the source sequence is empty, then use this answer (I adjusted it a bit differently for your LINQ usage):
Max or Default? 

int iNumber = iContext. DetailsRecords. Select(x => (int?)x. Number).Max() ??  0;

Enumerable. Max Method (IEnumerable<Int32>): InvalidOperationException - 
source contains no elements. 
Enumerable. Max Method (IEnumerable<Nullable<Int32>>): If the source sequence is empty or contains only values that are null, this function returns null. 

A:

You can use DefaultIfEmpty for this.   If the sequence is empty it will return the provided item as the only item in the sequence, if not it will return the original sequence. 
IEnumerable<int> numbers = new int [] { };

numbers. DefaultIfEmpty(0).Max();

