Language Integrated Query (LINQ) is a powerful feature in C# that allows developers to write queries against collections of objects in a concise and expressive manner. LINQ to Objects enables you to query any IEnumerable<T> collection, such as lists, arrays, or dictionaries, using a syntax similar to SQL.
In this tutorial, we will explore how to use LINQ to query and manipulate data within collections. We'll cover basic querying techniques, filtering, sorting, grouping, and transforming data.
LINQ provides a set of standard query operators that can be used to perform various operations on collections. These operators are available in the System.Linq namespace and include methods like Where, Select, OrderBy, GroupBy, and many more.
Let's dive into some practical examples to understand how these operators work.
Suppose we have a list of integers and we want to find all numbers greater than 10.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 5, 12, 8, 13, 6, 9, 11 };
var result = from num in numbers
where num > 10
select num;
foreach (var num in result)
{
Console.WriteLine(num);
}
}
}
<OutputBlock>
{`12
13
11`}
</OutputBlock>
In this example, the `Where` clause filters the numbers greater than 10, and the `Select` clause projects each number into a new form (in this case, the same number).
### Using Method Syntax
LINQ also supports method syntax, which can be more readable for complex queries.
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 5, 12, 8, 13, 6, 9, 11 };
var result = numbers.Where(num => num > 10);
foreach (var num in result)
{
Console.WriteLine(num);
}
}
}
<OutputBlock>
{`12
13
11`}
</OutputBlock>
### Sorting
To sort a collection, you can use the `OrderBy` method.
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David" };
var sortedNames = from name in names
orderby name.Length descending
select name;
foreach (var name in sortedNames)
{
Console.WriteLine(name);
}
}
}
<OutputBlock>
{`Charlie
Alice
David
Bob`}
</OutputBlock>
In this example, the `OrderBy` clause sorts the names by their length in descending order.
### Grouping
To group elements by a key, you can use the `GroupBy` method.
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<string> words = new List<string> { "apple", "banana", "cherry", "date", "elderberry" };
var groupedWords = from word in words
group word by word[0] into g
select new { FirstLetter = g.Key, Words = g };
foreach (var group in groupedWords)
{
Console.WriteLine($"First Letter: {group.FirstLetter}");
foreach (var word in group.Words)
{
Console.WriteLine(word);
}
}
}
}
First Letter: a apple First Letter: b banana First Letter: c cherry First Letter: d date First Letter: e elderberry
In this example, the GroupBy clause groups words by their first letter.
To transform data, you can use the Select method to project each element into a new form.
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 };
var squares = from num in numbers
select num * num;
foreach (var square in squares)
{
Console.WriteLine(square);
}
}
}
1 4 9 16 25
In this example, the Select clause projects each number into its square.
Now that you have a good understanding of LINQ to Objects, you can explore more advanced topics such as LINQ to SQL. LINQ to SQL allows you to query and manipulate data in relational databases using LINQ syntax, providing a seamless integration between your C# code and the database.
Stay tuned for more tutorials on querying and manipulating data with LINQ!