0
0
C Sharp (C#)programming~10 mins

LINQ with custom objects in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LINQ with custom objects
Create list of objects
Write LINQ query
Evaluate query: filter/map
Iterate results
Output filtered/mapped objects
Start with a list of custom objects, write a LINQ query to filter or select, then iterate and output the results.
Execution Sample
C Sharp (C#)
var people = new List<Person> {
  new Person("Alice", 30),
  new Person("Bob", 20),
  new Person("Charlie", 25)
};
var adults = people.Where(p => p.Age >= 21);
foreach(var adult in adults) Console.WriteLine(adult.Name);
This code filters a list of Person objects to find those aged 21 or older and prints their names.
Execution Table
StepActionEvaluationResult
1Create list 'people'List contains 3 Person objectspeople = [Alice(30), Bob(20), Charlie(25)]
2Apply Where filter p.Age >= 21Check Alice: 30 >= 21True, include Alice
3Apply Where filter p.Age >= 21Check Bob: 20 >= 21False, exclude Bob
4Apply Where filter p.Age >= 21Check Charlie: 25 >= 21True, include Charlie
5Iterate filtered resultsFirst: AliceOutput: Alice
6Iterate filtered resultsSecond: CharlieOutput: Charlie
7No more itemsIteration endsLoop exits
💡 All people checked; only those 21 or older included; iteration ends after last item.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7
peopleempty[Alice(30), Bob(20), Charlie(25)][Alice(30), Bob(20), Charlie(25)][Alice(30), Bob(20), Charlie(25)]
adultsnulldeferred query[Alice(30), Charlie(25)] (filtered)[Alice(30), Charlie(25)] (filtered)
Key Moments - 2 Insights
Why does 'adults' not immediately contain filtered results after Step 2?
LINQ uses deferred execution, so 'adults' holds the query but does not run it until iteration (see Steps 5-7).
Why is Bob excluded from the output even though he is in the original list?
Because Bob's age (20) does not satisfy the filter condition p.Age >= 21 (see Steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of evaluating Bob's age in Step 3?
ATrue, include Bob
BFalse, exclude Bob
CError, Bob missing age
DTrue, but exclude Bob
💡 Hint
Check Step 3 in the execution_table where Bob's age is compared to 21.
At which step does the program start outputting names?
AStep 5
BStep 4
CStep 2
DStep 7
💡 Hint
Look at the iteration steps in the execution_table where output happens.
If we change the filter to p.Age >= 20, how would the filtered list change?
AOnly Alice included
BAlice and Charlie included
CAlice, Bob, and Charlie included
DOnly Bob included
💡 Hint
Refer to variable_tracker and consider ages of all people compared to 20.
Concept Snapshot
LINQ with custom objects:
- Create a list of objects
- Use LINQ methods like Where to filter
- LINQ queries use deferred execution
- Iterate query results to get filtered objects
- Access object properties in lambda conditions
Full Transcript
This example shows how to use LINQ with a list of custom Person objects. First, we create a list with three people: Alice (30), Bob (20), and Charlie (25). Then, we write a LINQ query using Where to select only those with Age 21 or older. The query is not run immediately but waits until we iterate over it. When we loop through the filtered results, we print the names of Alice and Charlie because Bob does not meet the age condition. This demonstrates deferred execution and filtering with LINQ on custom objects.