Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Collection Utilities: LINQ and More | Sets and Collection Utilities
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Lists & Collections

bookCollection Utilities: LINQ and More

LINQ, or Language Integrated Query, is a powerful feature in C# that allows you to perform complex queries on collections easily and concisely. Some of the most commonly used LINQ methods are Where, Select, and OrderBy.

  • Where lets you filter items in a collection based on a condition;
  • Select projects each element into a new form, such as transforming or extracting part of the data;
  • OrderBy sorts the elements in a collection according to a specified key.

These methods can be chained together to create expressive and readable code for manipulating lists and other collections.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { public class Program { public static void Main() { List<int> numbers = new List<int> { 5, 2, 8, 3, 9, 4, 7 }; // Use LINQ to filter even numbers and sort them var evenNumbersSorted = numbers .Where(n => n % 2 == 0) .OrderBy(n => n) .ToList(); Console.WriteLine("Even numbers, sorted:"); foreach (var num in evenNumbersSorted) { Console.WriteLine(num); } } } }

LINQ queries work by chaining together methods that operate on collections, allowing you to describe what you want to retrieve rather than how to retrieve it. This approach leads to code that is easier to read, write, and maintain. LINQ methods are designed to work with any collection that implements IEnumerable, including lists, arrays, and sets.

The benefits of using LINQ include:

  • More concise code;
  • The ability to combine multiple operations in a single statement;
  • The flexibility to work with different types of data sources using the same query patterns.
Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829
// File: Program.cs using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { public class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 2, 5, 3, 6, 7, 3 }; // Find duplicates using LINQ var duplicates = numbers .GroupBy(n => n) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); Console.WriteLine("Duplicate numbers:"); foreach (var num in duplicates) { Console.WriteLine(num); } } } }

1. What does the Where method do in LINQ?

2. How can you sort a list using LINQ?

3. Fill in the blanks to select even numbers from a list using LINQ.

question mark

What does the Where method do in LINQ?

Select the correct answer

question mark

How can you sort a list using LINQ?

Select the correct answer

question-icon

Fill in the blanks to select even numbers from a list using LINQ.

[2, 4, 6]

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me an example of a LINQ query in C#?

What are some common use cases for LINQ in real-world applications?

How does LINQ compare to traditional loops in terms of performance?

bookCollection Utilities: LINQ and More

Stryg for at vise menuen

LINQ, or Language Integrated Query, is a powerful feature in C# that allows you to perform complex queries on collections easily and concisely. Some of the most commonly used LINQ methods are Where, Select, and OrderBy.

  • Where lets you filter items in a collection based on a condition;
  • Select projects each element into a new form, such as transforming or extracting part of the data;
  • OrderBy sorts the elements in a collection according to a specified key.

These methods can be chained together to create expressive and readable code for manipulating lists and other collections.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { public class Program { public static void Main() { List<int> numbers = new List<int> { 5, 2, 8, 3, 9, 4, 7 }; // Use LINQ to filter even numbers and sort them var evenNumbersSorted = numbers .Where(n => n % 2 == 0) .OrderBy(n => n) .ToList(); Console.WriteLine("Even numbers, sorted:"); foreach (var num in evenNumbersSorted) { Console.WriteLine(num); } } } }

LINQ queries work by chaining together methods that operate on collections, allowing you to describe what you want to retrieve rather than how to retrieve it. This approach leads to code that is easier to read, write, and maintain. LINQ methods are designed to work with any collection that implements IEnumerable, including lists, arrays, and sets.

The benefits of using LINQ include:

  • More concise code;
  • The ability to combine multiple operations in a single statement;
  • The flexibility to work with different types of data sources using the same query patterns.
Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829
// File: Program.cs using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { public class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 2, 5, 3, 6, 7, 3 }; // Find duplicates using LINQ var duplicates = numbers .GroupBy(n => n) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); Console.WriteLine("Duplicate numbers:"); foreach (var num in duplicates) { Console.WriteLine(num); } } } }

1. What does the Where method do in LINQ?

2. How can you sort a list using LINQ?

3. Fill in the blanks to select even numbers from a list using LINQ.

question mark

What does the Where method do in LINQ?

Select the correct answer

question mark

How can you sort a list using LINQ?

Select the correct answer

question-icon

Fill in the blanks to select even numbers from a list using LINQ.

[2, 4, 6]

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 4
some-alt