Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Why Two Identical LINQ Queries Return Rows in a Different Order Each Time

- 05.09.26 - ErcanOPAK

🔧 Same Query, Same Data – a Different Row Order Every Time You Run It

A LINQ query against Entity Framework returns the exact same set of rows on every run, just shuffled into a different order each time – no ORDER BY was written on purpose, but the assumption was that the database would at least be CONSISTENT about the order it left rows in.

🐞 The Problem

var orders = await db.Orders
    .Where(o => o.CustomerId == customerId)
    .ToListAsync();

// Run 1: returns rows in order [102, 105, 101, 103]
// Run 2, same data, same query: [105, 101, 103, 102]
// - a UI list built on this "just moves around" for no
//   apparent reason between page loads.

🔍 Why This Happens

SQL, and by extension Entity Framework, makes NO GUARANTEE about
row order unless an explicit ORDER BY is specified - the relational
model is fundamentally a set of rows, not a sequence. Without an
ORDER BY, the database is free to return rows in whatever order is
most convenient internally - influenced by index usage, physical
storage layout, parallel execution plans, or even just which CPU
core happened to scan which data page first. It can appear stable
for a long time purely by coincidence, then change the moment an
index changes or the query plan is recompiled.

✅ The Fix: Always Add an Explicit OrderBy for Anything User-Visible

  • Add .OrderBy(o => o.Id) (or whatever column defines the order you actually want) to every query whose result order matters to a human or to downstream logic – never rely on implicit database ordering, even if it ‘seems to always work.’
  • For paginated results specifically, an explicit, stable ORDER BY isn’t just cosmetic – without one, using .Skip()/.Take() across separate requests can return duplicate OR missing rows if the underlying implicit order shifts between calls.
  • If a specific column doesn’t guarantee a unique order (ties on the same value), add a secondary tiebreaker column (like Id) to the OrderBy so the order is fully deterministic, not just ‘usually deterministic.’

📦 Correct Version

var orders = await db.Orders
    .Where(o => o.CustomerId == customerId)
    .OrderBy(o => o.CreatedDate)
    .ThenBy(o => o.Id) // tiebreaker for a fully deterministic order
    .ToListAsync();

A database was never promising to remember the order you’re used to seeing — without an explicit ORDER BY, ‘the order it usually comes back in’ was always just a coincidence waiting to change.

— C# Best Practice

Related posts:

C#: Properties vs Fields — Know the Difference

C#: Properties vs Fields — Know the Difference

C#: Use File-Scoped Namespaces to Reduce Indentation

C#: Use File-Scoped Namespaces to Reduce Indentation

C#: Use Deconstruction with Tuples for Cleaner Multiple Returns

C#: Use Deconstruction with Tuples for Cleaner Multiple Returns

Views: 9
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

CSS: Fix an Image That Won’t Center No Matter What Margin or Text-Align You Try
C#: Fix a Switch Expression That Silently Matches the Wrong Case Because of Pattern Order

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

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

Social

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