0Pricing
C# Academy · Lesson

Using LINQ for Data Queries

Simplify complex data queries with LINQ (Language Integrated Query).

Using LINQ for Data Queries is a free C# Academy lesson on CoddyKit — lesson 5 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Using LINQ for Data Queries

Welcome to the next lesson! In this lesson, you’ll learn about LINQ (Language Integrated Query), a powerful feature in C# that simplifies data querying and manipulation. Let’s get started!

Using LINQ for Data Queries — illustration 1

2

What is LINQ?

LINQ (Language Integrated Query) is a set of features in C# that provides a consistent way to query data from various sources, such as collections, databases, XML, and more.

Key Point: LINQ integrates data querying into C# syntax, making it intuitive and easy to use.

3

Basic LINQ Query

LINQ queries are written using query syntax or method syntax. Example:

Tip: Use query syntax for readability and method syntax for flexibility.

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        // LINQ query syntax
        var evenNumbers = from num in numbers
                          where num % 2 == 0
                          select num;

        Console.WriteLine("Even Numbers:");
        foreach (var num in evenNumbers) {
            Console.WriteLine(num);
        }
    }
}

4

LINQ with Method Syntax

Method syntax is another way to write LINQ queries, using extension methods. Example:

Key Point: Method syntax is commonly used for more complex queries.

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        // LINQ method syntax
        var evenNumbers = numbers.Where(num => num % 2 == 0);

        Console.WriteLine("Even Numbers:");
        foreach (var num in evenNumbers) {
            Console.WriteLine(num);
        }
    }
}

5

LINQ with Collections

LINQ works seamlessly with collections like lists. Example:

Tip: Use LINQ with collections to simplify data manipulation tasks.

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

class Program {
    static void Main() {
        List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David" };

        // LINQ query to filter names
        var filteredNames = names.Where(name => name.StartsWith("C"));

        Console.WriteLine("Names starting with 'C':");
        foreach (var name in filteredNames) {
            Console.WriteLine(name);
        }
    }
}

6

Sorting Data with LINQ

Use OrderBy and OrderByDescending to sort data. Example:

Key Point: LINQ makes sorting operations simple and intuitive.

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = { 5, 1, 8, 3, 9 };

        // LINQ query to sort numbers
        var sortedNumbers = numbers.OrderBy(num => num);

        Console.WriteLine("Sorted Numbers:");
        foreach (var num in sortedNumbers) {
            Console.WriteLine(num);
        }
    }
}

7

Practice Challenge

Create a program that filters a list of products by price, sorts them by name, and displays the results using LINQ. Example:

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

class Program {
    static void Main() {
        var products = new List<Product> {
            new Product { Name = "Laptop", Price = 1000 },
            new Product { Name = "Phone", Price = 500 },
            new Product { Name = "Tablet", Price = 300 }
        };

        // LINQ query to filter and sort products
        var filteredProducts = products
            .Where(p => p.Price > 400)
            .OrderBy(p => p.Name);

        Console.WriteLine("Filtered and Sorted Products:");
        foreach (var product in filteredProducts) {
            Console.WriteLine($"{product.Name}: ${product.Price}");
        }
    }
}

class Product {
    public string Name { get; set; }
    public double Price { get; set; }
}

8

9

Best Practices for Using LINQ

Here are some best practices:

  • Use LINQ for concise and readable queries.
  • Prefer method syntax for complex queries.
  • Combine LINQ with lambda expressions for powerful data manipulations.

Tip: Test LINQ queries with different data to ensure accuracy.

10

Great Job!

Congratulations! You’ve learned how to use LINQ for querying and manipulating data in C#. This concludes the "Working with Files and Data" category. Let’s move forward to new challenges!

Using LINQ for Data Queries — illustration 10

Frequently asked questions

Is the “Using LINQ for Data Queries” lesson free?

Yes — the full text of “Using LINQ for Data Queries” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Using LINQ for Data Queries”?

Simplify complex data queries with LINQ (Language Integrated Query). You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 5 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Using LINQ for Data Queries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading and Writing Files
  2. Working with JSON and XML
  3. Understanding Exception Handling
  4. Working with Databases
  5. Using LINQ for Data Queries
← Back to C# Academy