Composability tips
Keep LINQ composable: use small pure predicates/transforms, compose delegates, add tiny extension helpers, and return IEnumerable pipelines.
Composable LINQ overview
Goal: Make LINQ easy to grow.
- Write small pure predicates/transforms
- Compose functions (no side effects)
- Add tiny extension helpers
- Return IEnumerable<T> pipelines
Pure helpers reused
Extract tiny pure methods and pass them as method groups. Reuse keeps pipelines short and readable.
using System;
using System.Linq;
public class Program
{
static bool IsEven(int n) { return n % 2 == 0; } // pure predicate
static int Square(int n) { return n * n; } // pure transform
public static void Main(string[] args)
{
int[] xs = new int[] { 1, 2, 3, 4, 5, 6 };
var q = xs
.Where(IsEven) // reuse predicate
.Select(Square); // reuse transform
foreach (int v in q) Console.WriteLine(v); // 4 16 36
}
}