Kaushal Pareek
Geek Culture
Published in
3 min readJan 18, 2023

--

Photo by Arnold Francisca on Unsplash

In my previous article, I have shared a brief about LINQ. You can access it from below link.

Here I will focus on LINQ operations. LINQ provides a number of standard query operators that can be used to filter, sort, and transform data. Some of the most commonly used LINQ operations include:

  1. Filtering: The Where operator is used to filter a sequence based on a certain condition. For example, to retrieve all the elements from a list of integers that are greater than 5, we can use the following query:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.Where(number => number > 5);

Projection: The Select operator is used to project a sequence of elements into a new form. For example, to retrieve the square of all elements in a list of integers, we can use the following query:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.Select(number => number * number);

Sorting: The OrderBy and OrderByDescending operators are used to sort a sequence of elements based on a certain key. For example, to sort a list of strings in alphabetical order, we can use the following query:

List<string> words = new List<string> { "apple", "banana", "cherry", "date" };
var result = words.OrderBy(word => word);

Aggregation: The Count, Sum, Min, Max, and Average operators are used to perform various aggregate operations on a sequence of elements. For example, to calculate the sum of all elements in a list of integers, we can use the following query:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.Sum();

Grouping: The GroupBy operator is used to group a sequence of elements based on a certain key. For example, to group a list of integers by their remainder when divided by 3, we can use the following query:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.GroupBy(number => number % 3);

Joining: The Join operator is used to join two sequences based on a common key. For example, to join a list of products with a list of categories based on the category ID, we can use the following query:

List<Product> products = // ...
List<Category> categories = // ...
var result = products.Join(categories,
product => product.CategoryID,
category => category.ID,
(product, category) => new { Product = product, Category = category });

These are just a few examples of the many operations that are available in LINQ. It is a very powerful feature and can be used to write concise and expressive code to work with data.

--

--

Kaushal Pareek
Geek Culture

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