How To Use Data Partitioning Operations With Database Using LINQ

In this article, I will demonstrate how we can use skip, SkipWhile, take, TakeWhile operator of Language-Integrated Query (LINQ). Skip, SkipWhile, take, TakeWhile operators are part of partitioning data in Language-Integrated Query (LINQ).

Introduction

In this article, I will demonstrate how we can use skip, SkipWhile, take, TakeWhile operator of Language-Integrated Query (LINQ). Skip, SkipWhile, Take and TakeWhile operators are part of partitioning data in Language-Integrated Query (LINQ).Partitioning in LINQ refers to the operation of dividing an input sequence into two sections, without rearranging the elements, and then returning one of the sections.

  • Skip operator skips a specified number of elements in a collection and then returns the remaining elements. The number of items to skip is specified using the count parameter this method expects.

  • SkipWhile operator skips elements in a collection as long as the given condition specified by the predicate is true, and then returns the remaining elements.

  • Take operator retunes a specified number of elements from the start of the collection. The number of items to return is specified using the count parameter this expects.

  • TakeWhile operator returns elements from a collection as long as the given condition specified by the predication is true.


Step 1: Open SQL server 2014 and create table Student and insert some records

  1. CREATE TABLE [dbo].[Student](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Student_Name] [nvarchar](50) NULL,  
  4.     [Age] [int] NULL,  
  5.     [Course] [nvarchar](50) NULL,  
  6.  CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED   
  7. (  
  8.     [ID] ASC  
  9. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  10. ) ON [PRIMARY]  
  11.   
  12. GO  

Step 2: Open Visual Studio 2015 and create a new console application with a meaningful name.

Step 3: Add Entity Framework now. For that, right click on Models folder, select Add, then select New Item, then click on it.

Screenshot for adding entity framework 1

 

After clicking on new item, you will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name DBModels (this name is not mandatory you can give any name) and click on Add.

Screenshot for adding entity framework 2

 

After you click on "Add a window", the wizard will open, choose EF Designer from database and click next.

Screenshot for adding entity framework 3

 

After clicking on Next a window will appear. Choose New Connection. Another window will appear, add your server name if it is local then enter dot (.). Choose your database and click on OK.

Screenshot for adding entity framework 4

 

Connection will be added. If you wish save connect as you want. You can change the name of your connection below. It will save connection in web config then click on Next.

Screenshot for adding entity framework 5

 

After clicking on NEXT another window will appear choose database table name as shown in the below screenshot then click on Finish.

Screenshot for adding entity framework 6

 

Screenshot for adding entity framework 7

 

Entity framework will be added and respective class gets generated under Models folder.

Following class will be added

  1. namespace Partitioning_Data  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class Student  
  7.     {  
  8.         public int ID { get; set; }  
  9.         public string Student_Name { get; set; }  
  10.         public Nullable<int> Age { get; set; }  
  11.         public string Course { get; set; }  
  12.     }  
  13. }  

Step 4: Write a program to call database class

Example of Skip Operator

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace Partitioning_Data  
  6. {  
  7.     class Skip  
  8.     {  
  9.         static void Main()  
  10.         {  
  11.             using (DBModel db = new DBModel())  
  12.             {  
  13.                 IEnumerable<Student> listStudent = db.Students.ToList();  
  14.   
  15.                 Console.WriteLine("LIST OF STUDENT\n");  
  16.   
  17.                 foreach (var student in listStudent)  
  18.                 {  
  19.                     Console.WriteLine("ID:" + "\t" + student.ID + "\t" + "Student Name:" + student.Student_Name + "\t" + "Age:" + student.Age + "\t" + "Course:" + student.Course);  
  20.                 }  
  21.   
  22.                 Console.WriteLine();  
  23.                 Console.WriteLine("AFTER SKIP OPERATION (Skip first 2 records)\n");  
  24.   
  25.                 var result = listStudent.Skip(2);  
  26.   
  27.                 foreach (var stud in result)  
  28.                 {  
  29.                     Console.WriteLine("ID:" + "\t" + stud.ID + "\t" + "Student Name:" + stud.Student_Name + "\t" + "Age:" + stud.Age + "\t" + "Course:" + stud.Course);  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34. }  
output
 

Example of SkipWhile Operator

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace Partitioning_Data  
  6. {  
  7.     class SkipWhile  
  8.     {  
  9.         static void Main()  
  10.         {  
  11.             using (DBModel db = new DBModel())  
  12.             {  
  13.                 IEnumerable<Student> listStudent = db.Students.ToList();  
  14.   
  15.                 Console.WriteLine("LIST OF STUDENT\n");  
  16.   
  17.                 foreach (var student in listStudent)  
  18.                 {  
  19.                     Console.WriteLine("ID:" + "\t" + student.ID + "\t" + "Student Name:" + student.Student_Name + "\t" + "Age:" + student.Age + "\t" + "Course:" + student.Course);  
  20.                 }  
  21.   
  22.                 Console.WriteLine();  
  23.                 Console.WriteLine("AFTER SKIP WHILE OPERATION (Display records until string length become 2 records)n");  
  24.   
  25.                 IEnumerable<Student>result = listStudent.SkipWhile(s => s.Course.Length>2);  
  26.   
  27.                 foreach (var stud in result)  
  28.                 {  
  29.                     Console.WriteLine("ID:" + "\t" + stud.ID + "\t" + "Student Name:" + stud.Student_Name + "\t" + "Age:" + stud.Age + "\t" + "Course:" + stud.Course);  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34. }  
Output