-
Notifications
You must be signed in to change notification settings - Fork 2
CSharp LINQ CoreOperators
Joe Care edited this page Dec 22, 2025
·
1 revision
This lesson introduces a few core LINQ operators on in-memory collections.
Related wiki pages:
- LINQ basics:
CSharp-LINQ-Where-OrderBy.md - Collections:
CSharpBible-Libraries.md
Official docs:
using System;
using System.Collections.Generic;
using System.Linq;
List<int> numbers = new() { 1, 2, 3, 4, 5 };
List<string> names = new() { "Alice", "Bob", "Charlie", "David" };IEnumerable<int> doubled = numbers.Select(n => n * 2);
Console.WriteLine(string.Join(", ", doubled)); // 2, 4, 6, 8, 10Select transforms each element but keeps the same number of elements.
IEnumerable<string> namesStartingWithA = names.Where(n => n.StartsWith("A"));
Console.WriteLine(string.Join(", ", namesStartingWithA)); // AliceWhere keeps only elements that satisfy the predicate.
int countAll = numbers.Count(); // 5
int countEven = numbers.Count(n => n % 2 == 0); // 2int firstNumber = numbers.First(); // 1
string firstName = names.First(); // AliceUse FirstOrDefault if the sequence might be empty.
foreach (int n in numbers)
{
Console.WriteLine(n);
}For more about foreach and collections see Lesson14.12.2025.md (to be renamed to a foreach-focused title) and CSharp-Generics-Intro.md.
- Combine
Where,Select, andOrderByas shown inCSharp-LINQ-Where-OrderBy.md. - Apply these patterns to your own domain objects (e.g. lists of products, customers, etc.).