C#

LINQ Queries

Learn LINQ Queries in C# with a tiny example and expected output.

LINQ Queries is part of the C# roadmap. Use the example below and continue with the next topic.

Syntax
Console.WriteLine("Hello C#");
Example
Line by line
using System.Linq;
Imports a namespace.
var nums = new[]{1,2,3,4};
C# line.
var evens = nums.Where(n => n%2==0).ToArray();
C# line.
Console.WriteLine(string.Join(",", evens));
Prints a line to output.
// Expected Output: 2,4
C# line.
Real world
  • LINQ is used for querying collections and databases (EF Core) with a readable syntax.
Common mistakes
  • Calling ToList() too early and doing work in memory unnecessarily.
Best practices
  • Keep queries composable and materialize results only when needed.