LINQ (Language Integrated Query) in C# - Textnotes

LINQ (Language Integrated Query) in C#


LINQ provides a unified, declarative way to query data from collections, arrays, databases, XML, and more using C# syntax.

1. What is LINQ?

  1. LINQ = Language Integrated Query
  2. Enables querying collections and data sources like SQL databases using C# syntax.
  3. Provides type safety, compile-time checking, and IntelliSense support.

Benefits of LINQ:

  1. Simplifies data queries
  2. Reduces code complexity
  3. Works with different data sources

2. LINQ to Objects

LINQ queries on in-memory collections like arrays, lists, or dictionaries.


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

// Query syntax
var evenNumbers = from n in numbers
where n % 2 == 0
select n;

foreach (var num in evenNumbers)
Console.WriteLine(num);
}
}

Output:


2
4
6

3. LINQ to SQL

  1. LINQ can be used to query SQL databases via LINQ to SQL.
  2. Requires DbContext (Entity Framework) or LINQ to SQL classes.

// Example with Entity Framework
using (var context = new SchoolContext())
{
var students = from s in context.Students
where s.Age > 18
orderby s.Name
select s;

foreach (var student in students)
Console.WriteLine($"{student.Name}, {student.Age}");
}

4. LINQ Queries

4.1 Where

Filter elements.


var evens = numbers.Where(n => n % 2 == 0);

4.2 Select

Project elements.


var squares = numbers.Select(n => n * n);

4.3 OrderBy / OrderByDescending

Sort elements.


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

4.4 GroupBy

Group elements.


var grouped = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");

foreach (var group in grouped)
{
Console.WriteLine(group.Key);
foreach (var num in group)
Console.WriteLine(num);
}

4.5 Join

Join two collections.


var students = new[]
{
new { Id = 1, Name = "John" },
new { Id = 2, Name = "Alice" }
};

var marks = new[]
{
new { StudentId = 1, Score = 90 },
new { StudentId = 2, Score = 85 }
};

var query = from s in students
join m in marks on s.Id equals m.StudentId
select new { s.Name, m.Score };

foreach (var item in query)
Console.WriteLine($"{item.Name}: {item.Score}");

5. Lambda Expressions with LINQ

Lambda expressions simplify LINQ queries.


var evens = numbers.Where(n => n % 2 == 0).OrderBy(n => n);

Other examples:


var squares = numbers.Select(n => n * n);
var sum = numbers.Sum();
var max = numbers.Max();
var min = numbers.Min();

6. LINQ with Collections and Arrays

LINQ works on arrays, lists, dictionaries, and other collections.


int[] arr = { 1, 2, 3, 4, 5 };
var filtered = arr.Where(n => n > 3); // Array

List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
var aFruits = fruits.Where(f => f.StartsWith("A")); // List

Summary of Chapter 11:

  1. LINQ allows querying collections and databases in a declarative way.
  2. Supports Where, Select, OrderBy, GroupBy, Join, and other operations.
  3. Can be used with lambda expressions for concise queries.
  4. Works with arrays, lists, dictionaries, and database objects.
  5. Reduces code complexity and improves readability.