👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Take to Select Specific Data

Using LINQ Take to Select Specific Data

Author - Abdul Rahman (Bhai)

LINQ

26 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

In this article, let's learn about how to use Take and TakeWhile in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ Single to Select Single Data in Collections.

We can perform partition and select specific piece of data from beginning of a collection using LINQ. We're going to use Take() and TakeWhile() methods. Let's take a look at each of these methods and how they work.

Why we gonna do?

When listing the items, its not always possible to list all the items. Sometimes we need to limit and partition the items from database or need a pagination in UI to display subset of records for better UX (User Experience) as not all the datas are required everytime unless its a reporting task. So Take(n) allows you to take only specific items from beginning of collection. It takes number of items given as input and returns n number of records from collection.

How we gonna do?

Perform partition using Take


List<Product> products = GetProducts();

//Method Syntax
List<Product> partitionedProducts = products
                                    .Take(2)
                                    .ToList();
                    
//Query Syntax
List<Product> partitionedProducts = (from product in products select product)
                                    .Take(2)
                                    .ToList();
        
Demo Space

Perform partition using Take with Range syntax

We can also use the Range Operator with Take() like Take(2..4), Take(..3), Take(^2..^4) or Take(3..).


List<Product> products = GetProducts();

//Method Syntax
List<Product> partitionedProducts = products
                                    .Take(2..)
                                    .ToList();
                    
//Query Syntax
List<Product> partitionedProducts = (from product in products select product)
                                    .Take(2..)
                                    .ToList();
        
Demo Space

Perform conditional partition using TakeWhile

We can also extract data conditionally using TakeWhile while the condition evaluates to true. Note that Enumeration stops when the predicate function returns false for an element or when source contains no more elements.


List<Product> products = GetProducts();

//Method Syntax
List<Product> partitionedProducts = products
                                    .TakeWhile(product => product.Name.StartsWith("P"))
                                    .ToList();
                    
//Query Syntax
List<Product> partitionedProducts = (from product in products select product)
                                    .TakeWhile(product => product.Name.StartsWith("P"))
                                    .ToList();
        
Demo Space

Summary

In this article we learn't how to locate partition and extract specific data within collection using Take and TakeWhile. We saw that there's an option to supply range of values using range operator. We also saw how we can pass an condition to partition data. All these can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • Take
  • TakeWhile
  • Partition