When calling Max() on an IQueryable and there are zero records I get the following exception.
The cast to value type \'Int32\' failed because the materialize
You can use:
FromSqlRaw("Select ifnull(max(columnname),0) as Value from tableName");