Summary: in this tutorial, you’ll learn about what LINQ is, how it works, and why you should use it.
In programming, you often deal with data manipulation. Whether it’s projecting, sorting, filtering, or grouping data, these operations can be complex and error-prone.
Also, the data may come from various sources such as XML files, databases, third-party API, etc., which makes the data manipulation more complex. The reason is that you need to study a specific API for each data source.
Fortunately, you can use a powerful tool in .NET that can make data manipulation easier: LINQ (Language Integrated Query).
Introduction to LINQ
LINQ is a set of language extensions in C# that allow you to manipulate and analyze data in a declarative and concise manner.
LINQ allows you to write queries against various data sources using the common syntax and the same set of operators.
LINQ syntax and operators are like SQL (structured query language), but LINQ offers a more type-safe and object-oriented approach to querying and managing data.
LINQ leverages the following language features in C#:
- Lambda expressions
- Extension methods
- Anonymous types
- Query expression syntax
- Generics
yield
andvar
keywords
LINQ allows you to write queries in two ways:
- Query Syntax
- Method Syntax
The query syntax uses a SQL-like syntax with keywords like from, where
, select
, orderby
, groupby
while the method syntax uses a set of extension methods such as Where()
, Select()
, OrderBy()
, and GroupBy()
.
Query syntax example
The following example shows how to use LINQ to query the even numbers from a list of numbers using the query syntax:
using static System.Console;
var numbers = new List<int> { 1, 5, 2, 8, 9 };
var evenNumbers = from number in numbers
where number % 2 == 0
select number;
foreach (var number in evenNumbers)
{
WriteLine(number);
}
Code language: C# (cs)
How it works.
First, define a list of integers called numbers:
var numbers = new List<int> { 1, 5, 2, 8, 9 };
Code language: C# (cs)
Second, use the from
keyword to define a range variable called number
that represents each element in the numbers
list, the where
keyword to filter the range variable that selects only even numbers, and the select
keyword to project the selected numbers into a new sequence.
var evenNumbers = from number in numbers
where number % 2 == 0
select number;
Code language: C# (cs)
Third, write all the selected even numbers to the console using a foreach
loop:
foreach (var number in evenNumbers)
{
WriteLine(number);
}
Code language: C# (cs)
Output:
2
8
Code language: C# (cs)
Method syntax example
The following example rewrites the above program but uses the method syntax:
using static System.Console;
var numbers = new List<int> { 1, 5, 2, 8, 9 };
var evenNumbers = numbers
.Where(number => number % 2 == 0)
.Select(number => number);
foreach (var number in evenNumbers)
{
WriteLine(number);
}
Code language: C# (cs)
In this example, the program chains the Where()
and Select()
methods to select even numbers from the numbers list.
LINQ offers a useful set of operators for querying and manipulating data. The following are the most common ones:
Where()
– filter a sequence based on a condition.Select()
– projects each member of a sequence into a new form.OrderBy()
/OrderByDescending()
– sort the sequence in ascending or descending order based on a sort key.GroupBy()
– group the elements based on a key.
LINQ advantages
LINQ offers the following benefits:
- Expressiveness: LINQ allows you to write declarative and concise code for querying data, making it easier to understand and maintain.
- Type safety: LINQ queries are strongly typed. This means that the compiler will catch type errors at compile time, which reduces the errors that can discover at runtime only.
- Integrate objects, relational data, and XML files – you can use unified query syntax across data sources.
Summary
- LINQ stands for Language Integrated Query.
- LINQ provides a unified API for manipulating data across data sources including objects, relational databases, and XML files.