If
long max = query.Any() ? query.Max() : 0;
returns zero, then one of following is true:
- Query does not return any results
- Max value in query results is zero
First case is possible when you modify your table between defining query and getting max value from query. Remember - query
does not have any data. It's only query definition, and you will get data only when you execute query (e.g. call Any() or Max()).
Test:
List<long> table = new List<long> { 1, 2, 3 };
var query = from t in table select t; // query is not executed
table.Clear(); // modify data source before query is executed
Assert.False(query.Any()); // execute query on modified data source