Introduction
When working with data in C#, developers often need to filter, sort, and search data. Writing complex loops and conditions can make code difficult to read.
To solve this problem, C# introduced LINQ (Language Integrated Query).
LINQ allows developers to query collections like arrays, lists, and databases using simple syntax.
In this article, we will learn 10 important LINQ queries that every beginner should know, with simple examples.
What is LINQ?
LINQ stands for Language Integrated Query.
It allows developers to write data queries directly inside C# code.
Example without LINQ
List<int> numbers = new List<int>() { 5, 10, 15, 20 };
foreach (var n in numbers)
{
if (n > 10)
{
Console.WriteLine(n);
}
}
Example using LINQ
var result = numbers.Where(n => n > 10);
LINQ makes the code shorter and easier to read.
1. Select All Data
LINQ can retrieve all items from a list.
List<string> names = new List<string>()
{
"Rahul",
"Amit",
"Neha"
};
var result = names.ToList();
Output
2. Filter Data Using Where
The Where() method filters data based on a condition.
List<int> numbers = new List<int>() { 10, 20, 30, 40 };
var result = numbers.Where(x => x > 20);
Output
3. Select Specific Data
The Select() method returns specific fields.
Example:
var result = numbers.Select(x => x);
This selects values from the collection.
4. Sort Data Using OrderBy
We can sort data in ascending order.
List<int> numbers = new List<int>() { 50, 20, 10, 40 };
var result = numbers.OrderBy(x => x);
Output
5. Sort Data Using OrderByDescending
This sorts data in descending order.
var result = numbers.OrderByDescending(x => x);
Output
6. Get First Item
Use First() to retrieve the first element.
var result = numbers.First();
Output
7. Count Items
The Count() method counts the number of elements.
var total = numbers.Count();
Output
8. Check Condition Using Any
Any() checks whether any item satisfies a condition.
bool result = numbers.Any(x => x > 30);
Output
9. Get Maximum Value
Use Max() to find the largest value.
var maxValue = numbers.Max();
Output
10. Get Minimum Value
Use Min() to find the smallest value.
var minValue = numbers.Min();
Output
Real Example Using LINQ with Objects
Suppose we have a Student class.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
Create a list.
List<Student> students = new List<Student>()
{
new Student{Id=1,Name="Rahul",Marks=80},
new Student{Id=2,Name="Amit",Marks=60},
new Student{Id=3,Name="Neha",Marks=90}
};
Find students with marks greater than 70.
var result = students.Where(s => s.Marks > 70);
Output
Advantages of LINQ
LINQ provides several benefits.
Cleaner Code
Queries become easier to read.
Less Code
Developers write fewer lines of code.
Strong Typing
Errors are detected at compile time.
Easy Data Manipulation
Filtering and sorting become simple.
Conclusion
LINQ is one of the most powerful features in C#. It allows developers to query data using simple syntax and improves code readability.
In this article, we learned: