Getting Started with LINQ: An Introduction to Language Integrated Query in C#

Kaushal Pareek
Geek Culture
Published in
2 min readJan 18, 2023
Photo by Luca Bravo on Unsplash

LINQ (Language Integrated Query) is a set of features in C# that allows developers to write queries against various data sources using a consistent syntax. The data sources can be in-memory objects, databases, XML documents, and more.

LINQ queries are written using a combination of standard query operators and lambda expressions. The query is executed when it is enumerated, which means that the query is not executed until the results are needed.

Here is an example of a simple LINQ query that retrieves all the elements from a list of integers that are greater than 5:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = from number in numbers
where number > 5
select number;
foreach (int number in result)
{
Console.WriteLine(number);
}

This query can also be written using method syntax, which is an alternate way of writing LINQ queries:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.Where(number => number > 5);
foreach (int number in result)
{
Console.WriteLine(number);
}

LINQ provides a wide range of standard query operators that can be used to filter, sort, and transform data. It also supports a number of extension methods that can be used to perform additional operations on the query results.
I have shared more detailed information on LINQ operation in my next article. You can visit it from here:

--

--

Kaushal Pareek
Geek Culture

I have lot’s of interests and technology is one which aces that list.