Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a Nullable Value Type Comparison That Silently Skips Null Rows

- 06.09.26 - ErcanOPAK

🔧 Your Nullable Comparison Is Quietly Excluding Every Null Row

A LINQ filter compares a nullable int against a threshold – `x > 5` – and it looks correct, until you notice every row where the value is null just vanished from the results instead of showing up as unmatched the way you expected, because in C# any comparison against null with a relational operator evaluates to false, never to null.

🔎 The Problem

public class Order
{
    public int Id { get; set; }
    public int? DiscountPercent { get; set; }   // nullable - not every order has one
}

var highDiscountOrders = orders.Where(o => o.DiscountPercent > 20).ToList();

// An order with DiscountPercent == null is silently excluded here -
// (null > 20) evaluates to false, not "unknown" - there's no exception,
// no warning, just a result set that's missing rows you may have expected
// to see flagged as "needs review" instead of dropped entirely.

✅ Fix: Decide Explicitly What Null Should Mean

  • If null should count as “below threshold”, the code above is already doing the right thing – but say so explicitly with `o.DiscountPercent.GetValueOrDefault() > 20` or `(o.DiscountPercent ?? 0) > 20` so the next reader doesn’t have to know the null-comparison rule to understand intent.
  • If null should be treated as its own separate case (e.g., “pending” or “needs review”), filter for it explicitly: `o.DiscountPercent == null || o.DiscountPercent > 20`, or branch the two cases into separate lists entirely.

⚠️ The Rule to Remember

  • For nullable value types, `==` and `!=` treat null as a normal comparable value (`null == null` is true), but `<`, `>`, `<=`, `>=` all return false whenever either side is null – there is no three-valued logic here like in SQL’s `NULL` handling.
  • This asymmetry is exactly why a filter that reads correctly at a glance can silently drop rows – the bug is in what you didn’t consider, not in what the compiler let you write.

Null isn’t a value below zero – it’s the absence of one, and a `>` comparison that quietly treats it as “loses” is a decision, even when nobody made it on purpose.

— C# Best Practice

Related posts:

Understanding Stack and Heap

Understanding Stack and Heap

C#: Fix a Local Function Failing to Compile Over a Span Parameter With a Cannot Use a Result of ref ...

C#: Fix a Local Function Failing to Compile Over a Span Parameter With a Cannot Use a Result of ref ...

LINQ Looks Clean but Allocates Heavily

LINQ Looks Clean but Allocates Heavily

Views: 7

Post navigation

The AI Prompt That Reviews Your Database Backup and Restore Plan for Silent Gaps
WordPress: Recover a Page Built With a Page Builder That Now Shows Raw Shortcodes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com