Bird
0
0

You have a dictionary dict with string keys and int values. How can you create a new dictionary containing only entries with values greater than 10?

hard🚀 Application Q8 of 15
C Sharp (C#) - Collections
You have a dictionary dict with string keys and int values. How can you create a new dictionary containing only entries with values greater than 10?
AUse dict.Filter(kv => kv.Value > 10);
BUse LINQ: dict.Where(kv => kv.Value > 10).ToDictionary(kv => kv.Key, kv => kv.Value);
CUse dict.Select(kv => kv.Value > 10);
DUse dict.RemoveWhere(kv => kv.Value <= 10);
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct LINQ method for filtering

    Where filters entries by condition, ToDictionary converts result back to dictionary.
  2. Step 2: Analyze each option

    Use LINQ: dict.Where(kv => kv.Value > 10).ToDictionary(kv => kv.Key, kv => kv.Value); correctly filters and creates new dictionary; others use invalid or non-existent methods.
  3. Final Answer:

    Use LINQ: dict.Where(kv => kv.Value > 10).ToDictionary(kv => kv.Key, kv => kv.Value); -> Option B
  4. Quick Check:

    Filter dictionary with Where + ToDictionary [OK]
Quick Trick: Use LINQ Where and ToDictionary to filter dictionaries [OK]
Common Mistakes:
MISTAKES
  • Using non-existent Filter or RemoveWhere methods
  • Using Select without ToDictionary
  • Trying to modify dictionary while iterating

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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