''Nullable object must have a value.' for List in LINQ c#

In database I have next model tblWorkItem with next field:

public List<xWorkItemItemFailReason> WorkItemItemFailReasons { get; set; }

When I am trying to get tblWorkItem data from database

_db.tblWorkItem.Where(x=>x.WorkItemItemFailReasons != null)

I have next error

System.InvalidOperationException: 'Nullable object must have a value.'

How I can check if this field is not null?



Solution 1:[1]

Problem in xWorkItemItemFailReason class. It has some property of primitive type (int, long, ...) that is not nullable, but has no value in database table. Check them.

Solution 2:[2]

In your modal class have some not nullable field so when fetching from database it comes with null values. so make it nullable .public int? FieldName {Get;set;}

Solution 3:[3]

if its .net core 3.0 or earlier there is a known issue where

Simple query filter breaks simple projection https://github.com/dotnet/efcore/issues/13517

Solution 4:[4]

You should also make sure there are no data integrity problems with your foreign key relationships. A foreign key value in the parent table that doesn't exist in the child table will throw this exception too (e.g., you set the foreign key to 0 instead of null if there's no child, and no child record in the database has a key value of 0).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Backs
Solution 2 Ranjith.V
Solution 3 Ram
Solution 4 John R