Oscar
Oscar

Reputation: 1147

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.

Upvotes: 0

Views: 180

Answers (2)

mnsr
mnsr

Reputation: 12437

Basically, p.Parent_ObjectiveID cannot be null, (i.e. 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.

Upvotes: 0

Zenwalker
Zenwalker

Reputation: 1919

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..

Upvotes: 3

Related Questions