I have the following code. I'm getting error:
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
var MaxValue = (from t in context.ppatalukmasters where t.regioncode == model.regioncode select t.talukcode).Max();
How can i modify?

Satyaprakash SamantarayRank #101 · 21.1K reputationPosted Nov 21, 2023, 3:36 AM EST
There is null handling case is written. Suggest to use ?? operator that will be translated to COALESCE whenever there is a error that the generated SQL query returns an unexpected null value
You can try the below code and do changes on code as per ur requirement.
var MaxValue = (from t in context.ppatalukmasters where t.regioncode == model.regioncode select (int?)t.talukcode).Max() ?? 0;Garima BansalRank #1,554 · 1K reputationPosted Nov 21, 2023, 4:21 AM EST
Satyaprakash Samantaray thank you soo much, it works