A hands-on article with example

How To Convert a SQL Query Into C# LINQ

Edwin Klesman
8 min readFeb 24, 2023
How to convert SQL into C# LINQ manually. A step-by-step article showing you how. #sql #linq #csharp #linqmeup.

In this article, I’ll show you what the basic steps are for converting a SQL query into LINQ. You’ll learn the basic steps needed while we convert an example query.

In this article, it's assumed that you have a basic understanding of SQL, and know how to write C# code.

Introduction

Structured Query Language (SQL) is a powerful language for working with relational databases. It is widely used to retrieve and manipulate data from a variety of databases. However, when it comes to working with data in C# code, Language Integrated Query (LINQ) is a popular alternative to SQL. In this article, we will show how to manually convert a SQL query into LINQ, step-by-step.

Example SQL Query

Let’s begin with an example SQL query that includes a join, a couple of where conditions, and ordering. Here is the query:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
WHERE Orders.ShippedDate IS NULL AND Orders.Freight > 100
ORDER BY Orders.OrderDate DESC

Step-by-Step Conversion to LINQ

--

--

Edwin Klesman

Senior dev @Detacom | cross-platform mobile & web dev | Product Maker | SaaS | from app ideas to implementation | Owner eekayonline.com | Music: edsonkailes.com