What is LINQ in C#?

C# LINQ Basic Fresher1–2 Yrs2–3 Yrs Conceptual 2 min read

Quick Answer

LINQ (Language-Integrated Query) is a uniform way to query data — in-memory collections, databases, XML, JSON — using C# operators like Where, Select, OrderBy, GroupBy and Join, in either method syntax or query syntax. Most operators are deferred: nothing runs until you enumerate the result.

LINQ is a consistent query syntax built into C#. Whether I'm querying a list in memory, a database through EF Core, or an XML document, I use the same operators — Where, Select, OrderBy, GroupBy. There's a query syntax and a method syntax and they compile to the same code. The important behaviour is deferred execution: operators like Where and Select just build a pipeline and don't do anything until I enumerate it with something like ToList or a foreach. That means a query re-runs each time I iterate it and picks up changes to the source, so I call ToList when I want a fixed snapshot. On IQueryable, that laziness is what lets EF build one efficient SQL query.

LINQ operators are extension methods on IEnumerable<T> (LINQ to Objects) and IQueryable<T> (LINQ to a provider such as EF Core). The two syntaxes compile to the same thing:

var q1 = from p in products where p.Price > 100 select p.Name;
var q2 = products.Where(p => p.Price > 100).Select(p => p.Name);

Deferred vs immediate execution:

  • Deferred — Where, Select, OrderBy, Take: build a pipeline, run lazily.
  • Immediate — ToList, ToArray, Count, First, Sum: execute now.

Consequences: a deferred query re-runs every time you enumerate it, and it sees later changes to the source. Materialise with ToList() when you need a stable snapshot or will iterate multiple times. With IQueryable, deferred execution is what lets EF Core combine everything into one SQL statement.

C# — deferred execution
var numbers = new List<int> { 1, 2, 3 };

var evens = numbers.Where(n => n % 2 == 0);  // nothing runs yet

numbers.Add(4);

foreach (var n in evens) Console.Write(n);    // prints "24" — sees the new item

var snapshot = numbers.Where(n => n % 2 == 0).ToList(); // fixed now