LINQ in C# Explained In Five Minutes

Peterson C
The Startup
Published in
5 min readJan 10, 2020

--

Photo by National Cancer Institute on Unsplash

In most cases, regardless of which tech stack you use, you’ll have some kind of backend service to retrieve data when designing an application. The data can come from different sources. Each data source might have a specific language. If you have an application that retrieves data from four different sources, you’d have to learn all four languages to query the data. This is where LINQ comes to the rescue.

LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats. If you are familiar with SQL then this will seem very familiar. Some basic C# knowledge, version 7.0 and above, is required to follow along. I am to use Visual Studio on a mac. It can be downloaded for windows and mac here.

Open visual studio, and select Console Project as your template, then name your project linqDemo. For brevity, I’ve written a Car class with a constructor and a static method that returns a collection of cars, which we will use as our data source. I’m going to cover some of the most common features, such as SELECT, WHERE, ORDERBY and GROUPBY.

Replace your entire Program.cs file with this code.

using System;
using System.Collections.Generic;
using System.Linq;
namespace linqDemo
{
class MainClass
{
public static…

--

--

Peterson C
The Startup

Husband, father, engineer, musician, and writer by luck. I write mostly about whatever comes to mind. Follow me on this crazy writing journey if you dare.