LINQ Introduction

LINQ Introduction


6 Articles


In this article, let's learn about LINQ in .NET.

Table of Contents

  1. Assumptions
  2. Pre Requisites
  3. Introduction
  4. Examples SQL, Loops and LINQ
  5. Why use LINQ?
  6. LINQ Operations
  7. Summary


I assumne you are,

  • a C# developer
  • familiar with SQL
  • new to using LINQ

Pre Requisites

  • C# Generics
  • C# Delegates, Lambda Expressions
  • C# Extension Methods


Language-Integrated Query LINQ is a SQL‑like syntax that you can use in C# and Visual Basic and allows you to query any type of collections that implement IEnumerable<T> or IQueryable<T>.

Now, some of the most common IEnumerable types are,

  • Any Array
  • String (Array of characters)
  • List<T>
  • HashSet<T>, Dictionary<TKey, TValue>, LinkedList<T>, etc

The IQueryable objects are typically those found in LINQ integrations. Now, some of the most common IQueryable integrations are,

  • LINQ to XML - translator that knows how to read XML documents and then allow you to apply LINQ queries to that XML
  • Entity Framework - SQL LINQ translator that reads from a SQL database and then gives you back that data that you can now query

Besides the LINQ integrations, there's LINQ to objects

  • LINQ and Strings
  • LINQ and Reflection
  • LINQ and File Directories
  • LINQ to Entities
  • LINQ to Dataset

To use LINQ in your applications, all you need to do is add a using statement using System.Linq. What this does is this adds extension methods that are part of the Enumerable and Queryable base classes so that you can apply the LINQ queries to whatever you're going to do.

LINQ in DotNet

Examples SQL, Loops and LINQ

Let's take a quick comparison of SQL, loops, and LINQ. SQL is very similar to the LINQ query language, so we're going to take a look at SQL, looping, and LINQ all side by side. Let's start out with a simple SQL WHERE clause.

Code Sample - simple SQL WHERE clause

Code Sample - simple SQL MIN clause

Why use LINQ?

So, why use LINQ? Because,

  • Unified approach for querying any types of object
  • Eliminate looping code
  • IntelliSense Support
  • Type-checking of objects at compile time
Why Use LINQ in DotNet

LINQ Operations

So what are the types of operations that we can perform with LINQ? Well, we can

  • Select
  • Projection (change shape)
  • Order (ascending / descending)
  • Get an Element (find, first, last, single)
  • Filter (where)
  • Iteration / Partition (foreach, skip, take)
  • Quantify (any, all, contains)
  • Set Comparison (equal, except, intersection)
  • Set Operations (union, concat)
  • Joining (inner joins, outer joins)
  • Grouping (groupby, subquery, groupjoin)
  • Distinct Sets (distinct)
  • Aggregation (count, sum, min, max, average)
LINQ Operations in DotNet


In this article we learn't what is LINQ. It's a SQL-like syntax for C# used to query IEnumerable and IQueryable types. It can be used with many types of collections. It can search, order, group, etc and do many other operations. In the next article we will explore each operations with a demo.

  • Linq
  • Language Integrated Querying