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

LINQ query syntax in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LINQ query syntax
Start with data source
Write query expression
Use from, where, select clauses
Query is not executed yet
Execute query by foreach or ToList()
Get filtered and projected results
LINQ query syntax starts with a data source, then you write a query using from, where, and select clauses. The query runs when you iterate or convert it.
Execution Sample
C Sharp (C#)
int[] numbers = {1, 2, 3, 4, 5};
var evens = from n in numbers
            where n % 2 == 0
            select n;
foreach(var num in evens) Console.WriteLine(num);
This code selects even numbers from an array and prints them.
Execution Table
StepActionEvaluationResult
1Start with numbers arraynumbers = {1,2,3,4,5}Data source ready
2Define query: from n in numbersn takes each numberQuery prepared but not run
3Apply where n % 2 == 0Check each n: 1%2=1 (false), 2%2=0 (true), 3%2=1 (false), 4%2=0 (true), 5%2=1 (false)Filter applied
4Select nSelect numbers passing whereSelected: 2,4
5Execute query with foreachIterate over selected numbersOutput: 2 4
6EndNo more numbersQuery complete
💡 All numbers checked; query executed and results printed
Variable Tracker
VariableStartAfter Step 3After Step 4Final
numbers{1,2,3,4,5}{1,2,3,4,5}{1,2,3,4,5}{1,2,3,4,5}
nundefined1,2,3,4,5 (each in turn)2,4 (filtered)2,4 (selected)
evensundefinedquery definedquery defined2,4 (after execution)
Key Moments - 3 Insights
Why doesn't the query run immediately after defining it?
The query is only defined at first (see Step 2 in execution_table). It runs later when we iterate over it (Step 5). This is called deferred execution.
What does the 'where' clause do in the query?
The 'where' clause filters the data. It checks each number and keeps only those where the condition is true (Step 3 shows how numbers are tested).
How does the 'select' clause affect the output?
The 'select' clause chooses what to output from each filtered item. Here it selects the number itself (Step 4), so output is the filtered numbers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'n' when the where condition is true for the first time?
A1
B2
C3
D4
💡 Hint
Check Step 3 in execution_table where each 'n' is tested against the condition.
At which step does the query actually run and produce output?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look for the step where foreach iterates and prints results in execution_table.
If we remove the where clause, what would be the output after execution?
AAll numbers 1 to 5
BOnly odd numbers
COnly even numbers
DNo output
💡 Hint
Without filtering (where), select outputs all numbers from the source.
Concept Snapshot
LINQ query syntax:
- Start with 'from' to pick data source
- Use 'where' to filter items
- Use 'select' to choose output
- Query runs when iterated (deferred execution)
- Example: from n in numbers where n%2==0 select n
Full Transcript
This visual trace shows how LINQ query syntax works in C#. We start with a data source array of numbers. We write a query using 'from' to pick each number, 'where' to filter even numbers, and 'select' to choose them. The query is defined but not run until we loop over it with foreach. Step by step, each number is checked against the condition. Only numbers passing the filter are selected. Finally, the query runs and prints the even numbers 2 and 4. Variables like 'n' change as each number is processed. This shows deferred execution and how query clauses work together.