Entity Framework
Querying Null Values
C# Programming
Database Queries
.NET Development

How can I query for null values in entity framework?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Querying for null in Entity Framework is usually straightforward, but developers often overcomplicate it because they are thinking in SQL syntax instead of LINQ translation rules. In most cases, you just write x.Property == null or x.Property != null and let EF translate that into SQL IS NULL or IS NOT NULL.

Basic Null Query in LINQ

Suppose you have an entity with a nullable column:

csharp
1public class User
2{
3    public int Id { get; set; }
4    public string? Email { get; set; }
5}

To find rows where Email is null:

csharp
1using var db = new AppDbContext();
2
3var users = db.Users
4    .Where(u => u.Email == null)
5    .ToList();

Entity Framework translates that to SQL using IS NULL.

For non-null rows:

csharp
var users = db.Users
    .Where(u => u.Email != null)
    .ToList();

That becomes IS NOT NULL.

Nullable Value Types Work the Same Way

If the property is a nullable value type such as DateTime? or int?, the LINQ expression is the same.

csharp
1public class Order
2{
3    public int Id { get; set; }
4    public DateTime? ShippedAt { get; set; }
5}
6
7var unshipped = db.Orders
8    .Where(o => o.ShippedAt == null)
9    .ToList();

The important thing is that the property must actually be nullable in the model if the database column can be null.

Avoid SQL-Style Null Thinking in LINQ

In SQL, people think in terms of:

sql
WHERE Email IS NULL

In LINQ to Entities, you do not write IS NULL. You write normal C# null comparisons and rely on translation.

That is one of the core ergonomic benefits of EF.

If you are ever unsure what is happening, inspect the generated SQL through logging rather than guessing.

Combine Null Checks with Other Conditions

Null checks compose naturally with other predicates.

csharp
var users = db.Users
    .Where(u => u.Email == null && u.IsActive)
    .ToList();

Or:

csharp
var overdue = db.Orders
    .Where(o => o.ShippedAt == null && o.CreatedAt < cutoffDate)
    .ToList();

You do not need a special EF-only syntax here. Plain boolean expressions are the right approach.

Things become more interesting when null is involved through optional relationships.

Suppose an order can have no invoice:

csharp
var ordersWithoutInvoice = db.Orders
    .Where(o => o.Invoice == null)
    .ToList();

EF can translate this, but optional relationship queries are worth checking carefully because the generated SQL may involve joins that are less obvious than scalar null checks.

If the behavior surprises you, inspect the SQL and the relationship mapping.

String Empty Is Not the Same as Null

A common mistake is to ask for null values but actually mean "null or empty."

This:

csharp
var users = db.Users.Where(u => u.Email == null).ToList();

does not include empty strings.

If you want both:

csharp
var users = db.Users
    .Where(u => u.Email == null || u.Email == "")
    .ToList();

Or, if the provider supports it cleanly:

csharp
var users = db.Users
    .Where(u => string.IsNullOrEmpty(u.Email))
    .ToList();

Be explicit about the business meaning. Null and empty string are not interchangeable unless your application says they are.

Async Query Example

The null predicate works the same in async EF queries.

csharp
1using Microsoft.EntityFrameworkCore;
2
3var users = await db.Users
4    .Where(u => u.Email == null)
5    .ToListAsync();

That is the preferred form in async web applications.

Common Pitfalls

  • Trying to write SQL syntax like IS NULL directly inside LINQ.
  • Forgetting that null and empty string are different conditions.
  • Defining a non-nullable CLR property for a nullable database column.
  • Assuming relationship-null queries behave exactly like scalar property null checks without verifying mapping.
  • Debugging the predicate without looking at the SQL EF actually generated.

Summary

  • In Entity Framework, query nulls with normal C# expressions such as x.Property == null.
  • EF translates those expressions into IS NULL or IS NOT NULL in SQL.
  • The same approach works for nullable reference types and nullable value types.
  • Be clear about whether you mean null only or null plus empty string.
  • When the result is surprising, inspect the generated SQL and the model mapping.

Course illustration
Course illustration

All Rights Reserved.