I need to return rows from the database that have "age" equal to null or zero.
The column is numeric and nullable, and the database is SQLServer.
My query in Entity Framework is as follows:
var query = _context.Student
.Where(Student => Student.Age == null || Student.Age == 0)
.AsNoTracking()
.ToQueryString();
And my query transcribed to SQL is as follows:
SELECT [i].[Name],
FROM [Student] AS [i]
WHERE [i].[Age] = 0.0
Note that the Entity Framework simply discards the "Age == null" clause, taking into account only the clause (Age == 0.0)
My Student class has the following attributes:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public decimal? Age { get; set; }
}
I've tried using several methods, such as HasValue, or GetValueOrDefault, but none seem to take into account the "is null" clause.
Note: it is not an option to change the column type "age" to "not null".
Edit: EF Version = Microsoft.EntityFrameworkCore 6.0.1