Skip to content

CSharp LINQ Async Overview

Joe Care edited this page Dec 22, 2025 · 1 revision

C# Overview: LINQ and Async/Await Together

This lesson briefly shows how LINQ and async/await can be combined in typical scenarios.

Related wiki pages:

  • LINQ basics: CSharp-LINQ-Where-OrderBy.md, Lesson10.12.2025.md
  • Async basics: CSharp-AsyncAwait-Basics.md, Lesson09.12.2025.md

1. LINQ over in-memory data

using System;
using System.Collections.Generic;
using System.Linq;

var numbers = new List<int> { 1, 2, 3, 4, 5 };

var evenSquares = numbers
    .Where(n => n % 2 == 0)
    .Select(n => n * n);

Console.WriteLine(string.Join(", ", evenSquares)); // 4, 16

2. Async method returning a sequence

using System.Collections.Generic;
using System.Threading.Tasks;

async Task<List<int>> GetNumbersAsync()
{
    await Task.Delay(500); // simulate async work
    return new List<int> { 1, 2, 3, 4, 5 };
}

3. Applying LINQ to async results

using System;
using System.Linq;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var numbers = await GetNumbersAsync();

        var greaterThanTwo = numbers.Where(n => n > 2).ToList();

        Console.WriteLine(string.Join(", ", greaterThanTwo)); // 3, 4, 5
    }

    static async Task<System.Collections.Generic.List<int>> GetNumbersAsync()
    {
        await Task.Delay(500);
        return new() { 1, 2, 3, 4, 5 };
    }
}

4. Next steps

  • Deepen your LINQ knowledge with the dedicated LINQ lessons.
  • Deepen async understanding with HTTP and file examples in Lesson09.12.2025.md and Lesson16.12.2025.md.

Clone this wiki locally