wasAsked.

LINQ and Lambda Expressions – Divisible-by-N examples

EPAM · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

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

Typical "divisible by N" exercises test Where, Select, method syntax and lambdas. Numbers divisible by 3 from a list: int[] numbers = [1, 3, 5, 6, 9, 10, 12, 15]; var byThree = numbers.Where(n => n % 3 == 0); // 3, 6, 9, 12, 15 Query syntax equivalent: var byThree2 = from n in numbers where n % 3 == 0 select n; Divisible by both 3 and 5: var byFifteen = numbers.Where(n => n % 3 == 0 && n % 5 == 0); // 15 Divisible by any value in a list of divisors: int[] divisors = [4, 6]; var matches = numbers.Where(n => divisors.Any(d => n % d == 0)); // 6, 12 A reusable predicate factory — a nice way to show lambdas returning lambdas: Func<int, bool> DivisibleBy(int n) => x => x % n == 0; var byFive = numbers.Where(DivisibleBy(5)); // 5, 10, 15 Generate a range and project: var squaresOfEvens = Enumerable.Range(1, 20) .Where(n => n % 2 == 0) .Select(n => new { Number = n, Square = n * n }); Count and sum: int countBy7 = Enumerable.Range(1, 100).Count(n => n % 7 == 0); // 14 int sumBy7 = Enumerable.Range(1, 100).Where(n => n % 7 == 0).Sum(); // 735 Classic FizzBuzz using LINQ: var fizzBuzz = Enumerable.Range(1, 15).Select(n => n % 15 == 0 ? "FizzBuzz" : n % 3 == 0 ? "Fizz" : n % 5 == 0 ? "Buzz" : n.ToString()); Console.WriteLine(string.Join(", ", fizzBuzz)); Group numbers by remainder: var groups = numbers.GroupBy(n => n % 3) .Select(g => $"remainder {g.Key}: {string.Join(",", g)}"); Points to explain while coding: - A lambda (n => n % 3 == 0) is an anonymous function, here a Func<int, bool> predicate. - LINQ uses deferred execution: Where does nothing until you enumerate (foreach, ToList, Count). - Guard against division by zero when N comes from user input. - Negative numbers: in C#, -6 % 3 == 0 is true, so they are handled correctly.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics