A lambda is an anonymous function. LINQ operators accept lambdas to filter, transform, order, and aggregate sequences. To return numbers divisible by `n`, validate that `n` is nonzero and use the remainder operator. ```csharp public static IEnumerable<int> DivisibleBy( IEnumerable<int> values, int divisor) { if (divisor == 0) throw new ArgumentOutOfRangeException(nameof(divisor)); return values.Where(value => value % divisor == 0); } var result = DivisibleBy(Enumerable.Range(1, 20), 3).ToArray(); // 3, 6, 9, 12, 15, 18 ``` `Where` is deferred: it stores the predicate and evaluates items when the result is enumerated. If the source can change and you need a snapshot, materialize with `ToArray` or `ToList`. For `IQueryable<T>`, the lambda becomes an expression tree and may be translated by the provider, so use expressions that provider supports. A simple loop may be clearer on a performance-critical hot path; measure rather than assuming LINQ is always too slow.
Krishna Yadav · · 0 likes