LINQ in C# – 15 Easy Examples for Beginners
Introduction
LINQ (Language Integrated Query) is one of the most powerful features in C#.
It enables you to query data directly within C# code using a clean and readable syntax.
You can use LINQ to work with:
- Collections (
List
,Array
, etc.) - Databases (Entity Framework)
- XML documents
- Remote APIs
In this tutorial, you’ll learn:
✅ What LINQ is and why it’s useful
✅ The basic syntax and query types
✅ 15+ practical examples for real-world use cases
1. What is LINQ?
LINQ provides a consistent way to query different data sources without needing to learn new query languages for each source.
Example without LINQ:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
List<int> evenNumbers = new List<int>();
foreach (int n in numbers)
{
if (n % 2 == 0)
evenNumbers.Add(n);
}
Example with LINQ:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
Cleaner, shorter, and more readable. ✅
2. LINQ Syntax Types
LINQ can be written in two ways:
1️⃣ Query Syntax (SQL-like)
var result = from n in numbers
where n % 2 == 0
select n;
2️⃣ Method Syntax (chained methods)
var result = numbers.Where(n => n % 2 == 0);
3. LINQ Examples in C#
We’ll use this sample list for most examples:
List<string> names = new List<string>
{
"John", "Sara", "Michael", "Samantha", "Bob", "Steve"
};
Example 1 – Filtering with Where
var result = names.Where(n => n.StartsWith("S"));
Example 2 – Sorting with OrderBy
var result = names.OrderBy(n => n);
Example 3 – Sorting Descending
var result = names.OrderByDescending(n => n);
Example 4 – Selecting Specific Data
var result = names.Select(n => n.ToUpper());
Example 5 – Anonymous Types
var people = new[]
{
new { Name = "John", Age = 30 },
new { Name = "Sara", Age = 25 }
};
var result = people.Select(p => new { p.Name, BirthYear = DateTime.Now.Year - p.Age });
Example 6 – Combining Where & OrderBy
var result = names.Where(n => n.Length > 3).OrderBy(n => n);
Example 7 – Distinct Values
var result = names.Distinct();
Example 8 – Any / All
bool hasS = names.Any(n => n.StartsWith("S"));
bool allLong = names.All(n => n.Length > 2);
Example 9 – First / FirstOrDefault
string firstName = names.First();
string safeName = names.FirstOrDefault(n => n.StartsWith("Z"));
Example 10 – Count & Sum
int total = names.Count();
int sumLength = names.Sum(n => n.Length);
Example 11 – Grouping Data
var grouped = names.GroupBy(n => n[0]);
foreach (var group in grouped)
{
Console.WriteLine($"Names starting with {group.Key}:");
foreach (var name in group)
Console.WriteLine(name);
}
Example 12 – Joining Two Lists
var ids = new[] { 1, 2, 3 };
var students = new[]
{
new { Id = 1, Name = "John" },
new { Id = 2, Name = "Sara" },
new { Id = 4, Name = "Steve" }
};
var joined = ids.Join(students,
id => id,
student => student.Id,
(id, student) => student.Name);
Example 13 – Take & Skip
var firstThree = names.Take(3);
var skipTwo = names.Skip(2);
Example 14 – Aggregate
string combined = names.Aggregate((a, b) => a + ", " + b);
Example 15 – Working with Numbers
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var squared = numbers.Select(n => n * n);
4. LINQ with Entity Framework
If you’re using Entity Framework, LINQ becomes even more powerful:
var students = context.Students
.Where(s => s.Age > 18)
.OrderBy(s => s.Name)
.ToList();
5. Best Practices for LINQ
✅ Prefer method syntax for chaining multiple operations
✅ Use ToList() only when necessary (to avoid unnecessary memory usage)
✅ Combine filters before fetching data (especially with databases)
Conclusion
LINQ in C# makes data queries simpler, cleaner, and more efficient.
From filtering and sorting to grouping and joining, you can write powerful queries with minimal code.
With practice, LINQ will become one of your favourite tools in C#. 🚀
FAQ
Q1. What is LINQ in C#?
A: LINQ in C# allows querying collections, databases, XML, and APIs using a consistent, readable syntax.
Q2. What are the types of LINQ syntax in C#?
A: LINQ supports Query Syntax (SQL-like) and Method Syntax (chained methods).
Q3. How do I use LINQ with Entity Framework?
A: You can combine LINQ queries with EF Core to filter, sort, group, and join database data.
Q4. Why should I learn LINQ in C#?
A: LINQ simplifies data queries, reduces code complexity, and improves readability and maintainability.