C# LINQ Where
Use LINQ to select valid workshop counts and materialize a result when a snapshot is required.
How it works
LINQ provides typed operations for asking questions of sequences. Where selects elements matching a predicate, while ToArray or ToList materializes the selected values. Keep the question visible in names and predicates instead of building a long chain whose purpose is difficult to explain. A filter can reject impossible counts, but a real import should usually also report rejected records rather than silently losing them. This fixed fixture deliberately separates selection from an input transport.
Run it locally
Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-buildUse the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.
Example
using System;
using System.Linq;
internal static class Program
{
private static void Main(string[] args)
{
int[] input = { 4, -1, 0, 12, 30 };
int[] valid = input.Where(count => count >= 0 && count <= 20).ToArray();
Console.WriteLine(string.Join(",", valid));
Console.WriteLine($"Accepted: {valid.Length}");
}
}
Expected result
4,0,12
Accepted: 3
The valid zero remains in the result, while negative and excessive counts are rejected. Materializing creates a new array of values, not a live view that re-evaluates the input. With reference-type elements, the array would still share those objects. Query providers translating expressions to databases have additional semantics; this course’s examples use in-memory LINQ to Objects.
Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.