Collections and LINQ in C#
In numerical programming, we rarely work with single values. Collections allow us to group related data—such as a vector of residuals or a list of material properties. LINQ (Language Integrated Query) provides a powerful, declarative way to filter, transform, and analyze these collections.
1. Common Collection Types
C# provides several specialized collections. While arrays are the standard for fixed-size mathematical data, other collections offer dynamic resizing and key-based lookups.
| Type | Namespace | Category | Description |
|---|---|---|---|
| Array (T[]) | System | Reference type | Fixed-size, high-performance contiguous memory. |
| List<T> | System.Collections.Gen | Reference type | Dynamically resizable array for iterative growth. |
| Dictionary<K,V> | System.Collections.Gen | Reference type | Key-value pairs for fast lookups. |
| HashSet<T> | System.Collections.Gen | Reference type | Unordered set of unique elements. |
Example 1C#
2. Introduction to LINQ
LINQ allows you to perform query operations directly on collections. It simplifies tasks like finding the maximum error in a vector or extracting specific nodes from a mesh using a functional approach.
Example 2C#
3. Deferred Execution
A vital concept in LINQ is that queries are not executed when they are defined. They are executed when you "materialize" them (by using foreach, .ToArray(), or .ToList()). This is known as lazy evaluation.
Examples
.. Admonition:: Example 1 : Filtering Convergence Data
In this scenario, we have a list of error values captured from a solver that is struggling to reach a steady state. We want to programmatically extract only the "successful" steps—those where the error dropped below our predefined tolerance—to verify how often our algorithm performed well.
Example 3C#
Found 2 converged steps.
.. Admonition:: Example 2 : Statistical Analysis
Imagine you have completed a simulation and possess a vector of residuals representing the difference between your guess and the true solution. Before proceeding, you need to calculate the average error to understand the general accuracy and the "Total Energy" (sum of squares) to assess the stability of the system.
Example 4C#
Average: 0.038, Energy: 0.0103
.. Admonition:: Example 3 : Mapping Node IDs
In Finite Element Analysis (FEA), nodes are often assigned unique identification numbers that aren't necessarily sequential. By using a Dictionary, we can map these arbitrary Node IDs to their physical coordinates on a 1D beam, allowing for instant lookup without searching through a massive list.
Example 5C#
Node 102 position: 0.5
.. Admonition:: Example 4 : Generating Sequences
Setup is a major part of numerical modeling. Instead of writing cumbersome loops to initialize an identity-like vector or an index map, we use LINQ generators to create a range of integers or a repeating set of initial guesses in a single line of code.
Example 6C#
Performance Note
While LINQ is expressive, it can introduce overhead due to allocations. For the "hot-path" of a numerical solver (like inner loops of matrix multiplication), traditional for-loops remain the preferred choice.