简体   繁体   中英

I got an error when comparing non-nullable type to null

I've got problems at this line:

int? nextLevel = (from p in cd.Objective
                  where p.Parent_ObjectiveID == null
                  select p.ObjectiveID).Max() + 1;

The error specifies:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

How can I fix it?

UPDATE:

But p.Parent_ObjectiveID is int? datatype. It can be null.

The objectiveID is of type which is not nullable. So whats the point of checking it for NULL type? You never can assign a null value to a non nullable value. May be use objectiveID? instead..

Basically, p.Parent_ObjectiveID cannot be null , (ie a "non-nullable" type). So there is no need to check if it is a null or not.

So to fix it, get rid of the where part, or change it from null , to an System.Int32 value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM