Why I Write My LINQ Queries Tall, Not Wide

code-qualitycsharplinq
Why I Write My LINQ Queries Tall, Not Wide

A tall LINQ query puts each method call in the chain on its own line, so the expression grows vertically instead of stretching across the screen. Each step becomes easier to read, and easier to follow into the next one in the chain.

Wishing You a Warm Welcome

First, I want to welcome you to the first edition of Milan's .NET Weekly newsletter.

I hope that this newsletter can become a positive force in the .NET community. To bring many of us together so that we can all continue learning and improving.

With that out of the way, let's get into .NET!

The Problem With Wide LINQ

Let's consider the following LINQ expression from a code style perspective.

I call this a wide LINQ expression because it stretches horizontally across the entire screen.

dbContext.Animals.Where(animal => animal.HasBigEars)
    .OrderBy(animal => animal.IsDangerous).Select(
        animal => (animal.Id, animal.Name)).ToList();
  • It is difficult to read.
  • It is difficult to reason about.
  • It is difficult to extend or maintain.

To improve this, I created a simple rule that you can follow:

When writing LINQ, try to go tall, not wide.

How to Write Tall LINQ

So how do we write tall LINQ expressions?

I'm going to rewrite the previous expression, to improve it.

Try to follow the one dot per line rule:

dbContext
    .Animals
    .Where(animal => animal.HasBigEars)
    .OrderBy(animal => animal.IsDangerous)
    .Select(animal => (animal.Id, animal.Name))
    .ToList();

Is the new version easier to read? Yes, very much so.

It is easier to understand what each expression does, and how it feeds into the next one in the chain.

If you are working in a team, try to propose this as a coding standard (if it isn't one already).
You will see that over time this will make a noticeable difference.


Frequently Asked Questions

What is a tall LINQ query?

A tall LINQ query puts each method call in the chain on its own line, so the expression grows vertically instead of stretching across the screen. Each step becomes easier to read and to follow into the next one in the chain.

How do you format long LINQ method chains in C#?

Follow the one dot per line rule: start from the source, then place every Where, OrderBy, Select, and ToList call on its own line. The chain then reads top to bottom, one operation at a time.

Why are wide LINQ expressions a problem?

A wide LINQ expression stretches horizontally across the entire screen. That makes it difficult to read, difficult to reason about, and difficult to extend or maintain, because you cannot quickly see what each operator in the chain does.

Should LINQ formatting be part of a team coding standard?

Yes. Proposing tall LINQ as a coding standard keeps the codebase consistent, and over time the improved readability makes a noticeable difference for everyone maintaining the code.

  • Functional Programming in C#: The Practical Parts

    Functional programming patterns can make your C# code safer and more maintainable, without getting lost in academic theory. Learn practical patterns you can use today to write better code.

  • 8 Tips To Write Clean Code

    Clean code is code that's easy to read, maintain, and understand. It's a skill you can learn with deliberate practice. My favorite way to practice is a refactoring exercise, so I prepared one for you: we'll improve one badly written method step by step.

  • How To Be a Better Software Engineer In 2023

    In this week's newsletter I will share 5 simple tips on how you can be a better software engineer in 2023. I find it a little amusing that the last newsletter of the year and is also coming out on the last day of the year. Here are 5 tips for being a better software engineer in 2023.

Loading comments...

Whenever you're ready, there are 5 ways I can help you:

  1. Pragmatic Self-Hosting: Learn to deploy and run your apps on a VPS with Dokploy and Tailscale. Keep administration private, automate releases, and practice restoring backups, with source code and a runbook you can reuse.
  2. Pragmatic Clean Architecture: Join 5,000+ students in this comprehensive course that will teach you the system I use to ship production-ready applications using Clean Architecture. Learn how to apply the best practices of modern software architecture.
  3. Modular Monolith Architecture: Join 2,800+ engineers in this in-depth course that will transform the way you build modern systems. You will learn the best practices for applying the Modular Monolith architecture in a real-world scenario.
  4. Pragmatic REST APIs: Join 1,900+ students in this course that will teach you how to build production-ready REST APIs using the latest ASP.NET Core features and best practices. It includes a fully functional UI application that we'll integrate with the REST API.
  5. Patreon Community: Join a community of 5,000+ engineers and software architects. You will also unlock access to the source code I use in my YouTube videos, early access to future videos, and exclusive discounts for my courses.

The .NET Weekly

One practical .NET tip, every Saturday.

Join 66,000+ engineers who read it. Free, and you can unsubscribe any time.