LINQ with custom objects in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using LINQ with custom objects, it's important to understand how the time to run queries changes as the number of objects grows.
We want to know how the program's work increases when we have more items to search or filter.
Analyze the time complexity of the following code snippet.
var people = new List<Person> {
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 35)
};
var result = people.Where(p => p.Age > 28).ToList();
This code filters a list of custom Person objects to find those older than 28 years.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
Wheremethod loops through each Person object to check the age condition. - How many times: It runs once for every person in the list.
As the number of people increases, the program checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of people.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the list gets bigger.
[X] Wrong: "LINQ queries run instantly no matter how many items there are."
[OK] Correct: LINQ still checks each item one by one, so more items mean more work and more time.
Understanding how LINQ works with custom objects helps you explain how your code handles data efficiently, a useful skill in many coding discussions.
"What if we changed the filter to check two conditions instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand LINQ's purpose
LINQ is designed to query collections like lists of objects easily.Step 2: Identify LINQ's main features
It helps filter, sort, and select data without manual loops.Final Answer:
Filter, sort, and select data easily -> Option CQuick Check:
LINQ = Filter, sort, select [OK]
- Thinking LINQ creates classes
- Confusing LINQ with compilation
- Assuming LINQ manages memory
Person objects using LINQ?Solution
Step 1: Understand Select syntax
Select expects a lambda expression to pick a property, likep => p.Name.Step 2: Check each option
var names = people.Select(p => p.Name); uses correct lambda syntax. var names = people.Select(p.Name); misses lambda. var names = people.Select(p => p); selects whole object. var names = people.Select(p => p.Name()); wrongly calls Name as method.Final Answer:
var names = people.Select(p => p.Name); -> Option DQuick Check:
Select needs lambda with property [OK]
- Omitting lambda arrow =>
- Calling property as method
- Selecting whole object instead of property
Person { public string Name; public int Age; } and list people with three persons: Alice(30), Bob(25), and Carol(35), what does this LINQ query return?var result = people.Where(p => p.Age > 28).Select(p => p.Name).ToList();
Solution
Step 1: Filter people older than 28
Alice is 30 (yes), Bob is 25 (no), Carol is 35 (yes).Step 2: Select their names
Names selected are "Alice" and "Carol".Final Answer:
["Alice", "Carol"] -> Option AQuick Check:
Age > 28 filters Alice and Carol [OK]
- Including Bob who is younger
- Selecting whole object instead of names
- Confusing > with <
var adults = people.Where(p => p.Age >= 18).Select(p => p.Name);
foreach(var name in adults) Console.WriteLine(name);
Solution
Step 1: Check LINQ query syntax
Where and Select are used correctly with proper lambda syntax.Step 2: Check foreach usage
LINQ returns IEnumerable<string>, which foreach can iterate without ToList().Final Answer:
The query is correct and will print all names of adults -> Option AQuick Check:
IEnumerable works with foreach directly [OK]
- Thinking ToList() is mandatory before foreach
- Misplacing Where and Select order
- Incorrect lambda syntax
Product objects with properties Name (string) and Price (decimal). How do you create a dictionary with product names as keys and prices as values, but only include products costing more than 50 using LINQ?Solution
Step 1: Filter products with price > 50
Use Where to keep only products costing more than 50.Step 2: Convert filtered list to dictionary
Use ToDictionary with key selector p.Name and value selector p.Price.Final Answer:
products.Where(p => p.Price > 50).ToDictionary(p => p.Name, p => p.Price); -> Option BQuick Check:
Filter then ToDictionary with key and value selectors [OK]
- Trying to filter after ToDictionary (invalid)
- Selecting anonymous types instead of original objects
- Calling ToDictionary without key/value selectors
