Chapter 19: LINQ (Language Integrated Query)

1. What is LINQ? (Super Simple Analogy)

Think of LINQ like Google search for your data:

  • You have a big list of students, products, orders, employees…
  • Instead of writing 10–20 lines of for loops, if conditions, sorting code…
  • You write one clean, readable line that says: “Give me all students from Hyderabad, sorted by age, show only their names”

LINQ does all the heavy lifting for you!

2. Two Ways to Write LINQ

There are two syntaxes – both do exactly the same thing:

Style Looks like Most people prefer because…
Query Syntax Like SQL (from … where … select) Very readable, especially for complex queries
Method Syntax Chained methods (.Where().OrderBy().Select()) Very powerful, great for chaining, lambdas

Important: Both are 100% equivalent – the compiler turns query syntax into method syntax behind the scenes.

3. Our Sample Data – Students List

Let’s use this list for all examples:

C#

4. Basic Filtering – Where

Query Syntax:

C#

Method Syntax:

C#

5. Ordering – OrderBy, ThenBy

Query Syntax:

C#

Method Syntax:

C#

Output:

text

6. Grouping – GroupBy

Query Syntax:

C#

Method Syntax:

C#

Output:

text

7. Joining – Join (Like SQL JOIN)

Example: Students + Cities with population

C#

Query Syntax (Inner Join):

C#

Method Syntax:

C#

8. Deferred Execution – The Magic of LINQ!

Very important concept:

Most LINQ queries do NOT execute immediately – they wait until you iterate over them (with foreach, .ToList(), .Count(), etc.).

C#

Why is this useful?

  • You can build a query step-by-step
  • It only runs once when needed
  • Very efficient!

Force execution immediately:

C#

Mini-Project: Student Report Generator with LINQ

C#

Output:

text

Summary – What We Learned Today

  • LINQ = Language Integrated Query – query data like SQL inside C#
  • Query syntax → looks like SQL (from … where … select)
  • Method syntax → chained methods (.Where().OrderBy().Select())
  • Filtering (Where), Ordering (OrderBy), Grouping (GroupBy), Joining (Join)
  • Deferred execution → query runs only when you iterate (foreach, .ToList(), etc.)
  • Anonymous types → new { s.Name, s.Marks } – perfect for temporary results

Your Homework (Super Fun & Practical!)

  1. Create a new console project called LinqMaster
  2. Create a list of Products with: Name, Price, Category, Stock
  3. Write LINQ queries (both syntaxes) to:
    • Get all products under ₹500 sorted by price
    • Group products by Category and show average price per category
    • Find products with stock < 10 (low stock alert)
    • Join with a list of Categories that have discounts
  4. Bonus: Use Take(5), Skip(3), FirstOrDefault(), Any(), All()

Next lesson: File I/O and Serialization – we’re going to learn how to read/write files, save data to JSON, and make your programs persistent!

You’re doing absolutely fantastic! 🎉 Any part confusing? Want more LINQ examples (grouping, joining, set operations)? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *