Bird
Raised Fist0

You have a list of students with their scores. You want to get a dictionary of students who scored above 70, with their names as keys and scores as values. Which LINQ query correctly does this?

hard🚀 Application Q15 of Q15
C Sharp (C#) - LINQ Fundamentals
You have a list of students with their scores. You want to get a dictionary of students who scored above 70, with their names as keys and scores as values. Which LINQ query correctly does this?
var students = new List<(string Name, int Score)>
{
    ("Alice", 85), ("Bob", 65), ("Charlie", 90)
};
var result = ???;
Astudents.Where(s => s.Score > 70).ToDictionary(s => s.Name, s => s.Score);
Bstudents.Select(s => s.Score > 70).ToDictionary(s => s.Name, s => s.Score);
Cstudents.ToDictionary(s => s.Name, s => s.Score > 70);
Dstudents.Where(s => s.Score > 70).Select(s => s.Name, s => s.Score);
Step-by-Step Solution
Solution:
  1. Step 1: Filter students with score > 70

    Use Where to select only students scoring above 70.
  2. Step 2: Convert filtered list to dictionary

    Use ToDictionary with key as Name and value as Score.
  3. Final Answer:

    students.Where(s => s.Score > 70).ToDictionary(s => s.Name, s => s.Score); -> Option A
  4. Quick Check:

    Filter then ToDictionary with correct keys and values [OK]
Quick Trick: Filter first, then convert to dictionary with keys and values [OK]
Common Mistakes:
MISTAKES
  • Using Select incorrectly instead of Where
  • Trying to create dictionary without filtering
  • Passing wrong arguments to ToDictionary

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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