Using LINQ Take to Select Specific Data
LINQ
26 Articles
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.
Table of Contents
- Introduction
- Perform partition using Take
- Perform partition using Take with Range syntax
- Perform conditional partition using TakeWhile
- Summary
Introduction
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.
Perform partition using Take
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.
Code Sample - LINQ Take
Demo - LINQ Take Clause Demo
Let's try LINQ Take
- We have
Product
class with following properties -Id, Name, Color, Price, Size
- Enter the number of products you would like to list.
- Click on Take Range Button
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
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..)
.
Code Sample - LINQ Take using Range Operator
Demo - LINQ Take Clause with Range Demo
Let's try LINQ Take
- We have
Product
class with following properties -Id, Name, Color, Price, Size
- Enter the number of products you would like to list.
- Click on Take Button
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
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.
Code Sample - LINQ TakeWhile
Demo - LINQ TakeWhile Clause Demo
Let's try LINQ TakeWhile
- We have
Product
class with following properties -Id, Name, Color, Price, Size
- Enter the condition based on which you like to select.
- Click on Take While Button
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
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.