LINQ in C#

What is LINQ?

LINQ stands for:

  • Language Integrated Query

In simple words:

👉 LINQ allows you to query data using C# code.

Instead of writing complex loops, we can write simple and clean queries.

Why LINQ Was Introduced?

Before LINQ:

  • We used for loop

  • We used foreach

  • We wrote many lines of code

  • Code was lengthy and confusing

LINQ makes:

  • Code shorter

  • Code readable

  • Code powerful

  • Easy filtering and sorting

Where Can We Use LINQ?

We can use LINQ with:

  • Arrays

  • Lists

  • Collections

  • Databases (Entity Framework)

  • XML

Example Without LINQ

List<int> numbers = new List<int> { 5, 10, 15, 20, 25 };

List<int> result = new List<int>();

foreach (int num in numbers)
{
    if (num > 10)
    {
        result.Add(num);
    }
}

👉 Many lines of code.

Same Example With LINQ

List<int> numbers = new List<int> { 5, 10, 15, 20, 25 };

var result = numbers.Where(n => n > 10);

foreach (var item in result)
{
    Console.WriteLine(item);
}

Output:

15
20
25
  • Clean

  • Simple

  • Easy to read

Important LINQ Methods (With Simple Explanation)

1. Where() – Filtering Data

Used to filter data.

var result = numbers.Where(n => n > 15);

✔ Returns only numbers greater than 15.

2. Select() – Selecting Specific Data

Used to select specific properties.

Example:

List<string> names = new List<string> { "Abhay", "Rahul", "Amit" };

var result = names.Select(n => n.ToUpper());

Output:

ABHAY
RAHUL
AMIT

3. OrderBy() – Sorting Ascending

var result = numbers.OrderBy(n => n);

4. OrderByDescending() – Sorting Descending

var result = numbers.OrderByDescending(n => n);

5. First() and FirstOrDefault()

First()

Returns first element.

var result = numbers.First();

⚠ If list empty → error.

FirstOrDefault()

var result = numbers.FirstOrDefault();

✔ If empty → returns default value (0 for int)

6. Count()

int total = numbers.Count();

7. Sum()

int totalSum = numbers.Sum();

8. Max() and Min()

int max = numbers.Max();
int min = numbers.Min();

LINQ Query Syntax vs Method Syntax

LINQ has two types:

Method Syntax (Commonly Used)

var result = numbers.Where(n => n > 10);

Query Syntax (SQL Like)

var result = from n in numbers
             where n > 10
             select n;

Both give same output.

But most developers use Method Syntax.

Real Example with Class

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Marks { get; set; }
}

Create List:

List<Student> students = new List<Student>
{
    new Student { Id = 1, Name = "Abhay", Marks = 80 },
    new Student { Id = 2, Name = "Rahul", Marks = 60 },
    new Student { Id = 3, Name = "Amit", Marks = 90 }
};

Get Students with Marks > 70

var result = students.Where(s => s.Marks > 70);

foreach (var student in result)
{
    Console.WriteLine(student.Name);
}

Output:

Abhay
Amit

Deferred Execution (Important Concept)

LINQ does not execute immediately.

It executes when:

  • You use foreach

  • You use ToList()

  • You use ToArray()

Example:

var result = numbers.Where(n => n > 10);

var finalList = result.ToList();

Now query runs.

Advantages of LINQ

  • Less code

  • Easy to understand

  • Strongly typed

  • Works with multiple data sources

  • Reduces bugs

Common Mistakes Beginners Do

  • Forgetting ToList() when needed

  • Using First() on empty list

  • Not understanding deferred execution

  • Writing complex queries in one line

Conclusion

In this article, we learned:

  • What is LINQ

  • Why we use LINQ

  • Important LINQ methods

  • Query syntax vs method syntax

  • Real example with class

  • Deferred execution