Linq: Nullable Object must have Value

Don Thomas Boyle

My query is as follows:

int ID = db.Q_Table.Find(item.PassedInID).ID;

I already found a solution for my issue, however i am wondering why i must write it like so

1.

Nullable(int) ID = db.Q_Table.Find(item.PassedInID).ID;

2.

db.Q_Table.Where(w => w.PassedInID== item.PassedInID).Select(s => s.ID ).SingleOrDefault();

It wouldn't let me put int int he < in the above code -.-...

I am curious why i have to code it to a nullable int? I really didn't want to code it like 2nd solution because its more code :). Yes i have made sure there are values in the database and from the below image you can see my database doesn't accept nulls.

Thanks for any answers

enter image description here

Habib

There is a difference between int and Nullable<int> or int?, you can't directly assign a Nullable<int> to int (Nullable<T>), Consider:

int? x = 123;
int y = x; //This would be an error

But you can use null-coalescing operator

int? x = 123;
int y = x ?? 0;

Now for your case, your ID seems to be a mapped to column in database which allows null. That will map to C# Nullable<int>, if you want to assign the result to an int you can do:

int ID = db.Q_Table.Find(item.PassedInID).ID ?? 0;

That will give your variable the value of ID or 0 if it is null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Nullable object must have a value datetime

From Dev

Decimal? - Nullable object must have a value

From Dev

"Nullable object must have a value" when Where uses a method?

From Dev

best method to solve Nullable object must have a value in datetime

From Dev

Nullable object must have a value. VB.NET

From Dev

Anonymous Type "Nullable object must have a value" Error

From Dev

"Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

From Dev

Nullable objects must have value error

From Dev

Nullable object must have a value while using datepicker control with dropdown control

From Dev

ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

From Dev

Linq sorting with nullable object

From Dev

Object must have some value in its @XmlValue field

From Dev

Must Be Mapped. It has no default value and is not nullable

From Dev

LINQ: Nullable value types in where clause

From Dev

LinQ: join on nullable property with value null

From Dev

Error setting keen.timestamp, value of property keen must be an object, is newtonsoft.json.linq.jproperty

From Dev

Error setting keen.timestamp, value of property keen must be an object, is newtonsoft.json.linq.jproperty

From Dev

Stripe error: "The card object must have a value for 'number'" when creating a Customer

From Dev

How to determine if a runtime object is of a nullable value type

From Dev

Linq At least one object must implement IComparable

From Dev

Expression must have a pointer to object type in C

From Dev

Array definition - Expression must have a constant value

From Dev

C++ Expression must have constant value

From Dev

c++ expression must have a constant value

From Dev

Intellisense expression must have a constant value

From Dev

block and expression must have a const value error

From Dev

Expression must have constant value Eigen matrix

From Dev

Can't set nullable int to LINQ query's return value?

From Dev

LINQ DB Arithmetic must have common type (Date Comparison

Related Related

  1. 1

    Nullable object must have a value datetime

  2. 2

    Decimal? - Nullable object must have a value

  3. 3

    "Nullable object must have a value" when Where uses a method?

  4. 4

    best method to solve Nullable object must have a value in datetime

  5. 5

    Nullable object must have a value. VB.NET

  6. 6

    Anonymous Type "Nullable object must have a value" Error

  7. 7

    "Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

  8. 8

    Nullable objects must have value error

  9. 9

    Nullable object must have a value while using datepicker control with dropdown control

  10. 10

    ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

  11. 11

    Linq sorting with nullable object

  12. 12

    Object must have some value in its @XmlValue field

  13. 13

    Must Be Mapped. It has no default value and is not nullable

  14. 14

    LINQ: Nullable value types in where clause

  15. 15

    LinQ: join on nullable property with value null

  16. 16

    Error setting keen.timestamp, value of property keen must be an object, is newtonsoft.json.linq.jproperty

  17. 17

    Error setting keen.timestamp, value of property keen must be an object, is newtonsoft.json.linq.jproperty

  18. 18

    Stripe error: "The card object must have a value for 'number'" when creating a Customer

  19. 19

    How to determine if a runtime object is of a nullable value type

  20. 20

    Linq At least one object must implement IComparable

  21. 21

    Expression must have a pointer to object type in C

  22. 22

    Array definition - Expression must have a constant value

  23. 23

    C++ Expression must have constant value

  24. 24

    c++ expression must have a constant value

  25. 25

    Intellisense expression must have a constant value

  26. 26

    block and expression must have a const value error

  27. 27

    Expression must have constant value Eigen matrix

  28. 28

    Can't set nullable int to LINQ query's return value?

  29. 29

    LINQ DB Arithmetic must have common type (Date Comparison

HotTag

Archive