If your column is not NULL and the result of your query is empty, you will receive an error message
"Casting the value type to" System.Int32 "failed because the materialized value is null. Either the parameter is a general result type or the query must use a type with a null value."
To avoid the error, you must direct the column to NULL, and the result from 0.
int max=(surveys.Max(g =>( int?)g.SurveyID) ?? 0);
. "Int32" , null