Language Integrated Query (LINQ) is a powerful feature introduced by Microsoft that allows developers to write queries against various data sources using a single, consistent syntax. LINQ provides a way to query and manipulate data from different types of collections such as arrays, lists, databases, XML documents, and more. This tutorial will introduce you to the basics of LINQ in C# and how it can simplify your code when dealing with data.
LINQ stands for Language Integrated Query, which means that querying is integrated into the language itself. Before LINQ, developers had to use different methods or libraries to query various types of data sources. LINQ unifies these approaches by providing a common syntax and set of operators that can be used across different data sources.
Let's start with some practical examples to understand how LINQ works in C#.
Suppose you have a list of integers, and you want to find all the even numbers greater than 5. Here’s how you can do it using LINQ:
1using System;2using System.Collections.Generic;3using System.Linq;45class Program6{7static void Main()8{9List<int> numbers = new List<int> { 1, 3, 6, 8, 9, 10, 12 };1011var query = from num in numbers12where num > 5 && num % 2 == 013select num;1415foreach (var number in query)16{17Console.WriteLine(number);18}19}20}
In this example:
from, where, and select keywords.foreach loop is used to iterate over the results of the query.6 8 10 12
Now, let's see how you can use LINQ to query an array of strings. Suppose you have an array of names and you want to find all the names that start with the letter "J":
1using System;2using System.Linq;34class Program5{6static void Main()7{8string[] names = { "John", "Jane", "Alice", "Jack", "Bob" };910var query = from name in names11where name.StartsWith("J")12select name;1314foreach (var name in query)15{16Console.WriteLine(name);17}18}19}
In this example:
foreach loop is used to print the results.John Jane Jack
LINQ can also be used to query lists of objects. Suppose you have a list of Person objects and you want to find all people who are older than 25:
1using System;2using System.Collections.Generic;3using System.Linq;45class Person6{7public string Name { get; set; }8public int Age { get; set; }9}1011class Program12{13static void Main()14{15List<Person> people = new List<Person>16{17new Person { Name = "Alice", Age = 28 },18new Person { Name = "Bob", Age = 22 },19new Person { Name = "Charlie", Age = 30 }20};2122var query = from person in people23where person.Age > 2524select person;2526foreach (var person in query)27{28Console.WriteLine($"{person.Name} is {person.Age} years old.");29}30}31}
In this example:
Person class with properties Name and Age.Person objects.Alice is 28 years old. Charlie is 30 years old.
In this tutorial, we introduced you to the basics of LINQ in C# and how it can be used to query different types of data sources. In the next section, we will explore "LINQ to Objects," which allows you to perform queries on collections such as lists and arrays.
By mastering LINQ, you will be able to write more concise and readable code when working with data in C#.