Bird
0
0

Given a list of integers var nums = new List<int> { 2, 4, 6, 8, 10 };, how can you find the sum of only the even numbers using aggregate functions?

hard🚀 Application Q9 of 15
C Sharp (C#) - LINQ Fundamentals

Given a list of integers var nums = new List<int> { 2, 4, 6, 8, 10 };, how can you find the sum of only the even numbers using aggregate functions?

Anums.Where(n => n % 2 == 0).Sum();
Bnums.Sum(n => n % 2 == 0);
Cnums.Count(n => n % 2 == 0);
Dnums.Average(n => n % 2 == 0);
Step-by-Step Solution
Solution:
  1. Step 1: Filter even numbers

    Use Where() to select numbers divisible by 2.
  2. Step 2: Sum filtered numbers

    Call Sum() on the filtered list to get total of even numbers.
  3. Final Answer:

    nums.Where(n => n % 2 == 0).Sum(); -> Option A
  4. Quick Check:

    Filter with Where(), then Sum() [OK]
Quick Trick: Use Where() to filter, then Sum() to add [OK]
Common Mistakes:
MISTAKES
  • Passing predicate directly to Sum()
  • Using Count() instead of Sum()
  • Using Average() incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes