Part 13 Implement paging using skip and take operators

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

LINQ Tutorial - All Text Articles & Slides

LINQ Tutorial Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

In this video, we will discuss implementing paging using Skip and Take operators in LINQ.

We will use the following Student class in this demo. Notice that, there are 11 total Students. We want to display a maximum of 3 students per page. So there will be 4 total pages. The last page, i.e Page 4 will display the last 2 students.
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public int TotalMarks { get; set; }

public static List[Student] GetAllStudetns()
{
List[Student] listStudents = new List[Student]
{
new Student
{
StudentID= 101, Name = "Tom", TotalMarks = 800
},
new Student
{
StudentID= 102, Name = "Mary", TotalMarks = 900
},
new Student
{
StudentID= 103, Name = "Pam", TotalMarks = 800
},
new Student
{
StudentID= 104, Name = "John", TotalMarks = 800
},
new Student
{
StudentID= 105, Name = "John", TotalMarks = 800
},
new Student
{
StudentID= 106, Name = "Brian", TotalMarks = 700
},
new Student
{
StudentID= 107, Name = "Jade", TotalMarks = 750
},
new Student
{
StudentID= 108, Name = "Ron", TotalMarks = 850
},
new Student
{
StudentID= 109, Name = "Rob", TotalMarks = 950
},
new Student
{
StudentID= 110, Name = "Alex", TotalMarks = 750
},
new Student
{
StudentID= 111, Name = "Susan", TotalMarks = 860
},
};

return listStudents;
}
}

Here is what we want to do
1. The program should prompt the user to enter a page number. The Page number must be between 1 and 4.
2. If the user does not enter a valid page number, the program should prompt the user to enter a valid page number.
3. Once a valid page number is entered, the program should display the correct set of Students

The following console application use Skip() and Take() operators to achieve this.
using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
class Program
{
public static void Main()
{
IEnumerable[Student] students = Student.GetAllStudetns();

do
{
Console.WriteLine("Please enter Page Number - 1,2,3 or 4");
int pageNumber = 0;

if (int.TryParse(Console.ReadLine(), out pageNumber))
{
if (pageNumber ]= 1 && pageNumber [= 4)
{
int pageSize = 3;
IEnumerable[Student] result = students.Skip((pageNumber - 1) * pageSize).Take(pageSize);

Console.WriteLine();
Console.WriteLine("Displaying Page " + pageNumber);
foreach (Student student in result)
{
Console.WriteLine(student.StudentID + "\t" + student.Name + "\t" + student.TotalMarks);
}
Console.WriteLine();
}
else
{
Console.WriteLine("Page number must be an integer between 1 and 4");
}
}
else
{
Console.WriteLine("Page number must be an integer between 1 and 4");
}
} while (1 == 1);
}
}
}

Please Note: The condition in the while loop puts the program in an infinite loop. To end the program, simply close the console window.
Рекомендации по теме
Комментарии
Автор

Very concise and informative ... Thank you man! :)

khaledsaleh
Автор

I think you have amazing talent of teaching. Everything you do to present your videos is amazing. Even the voice, the way you talk, the way you flip through screens...etc are just mind blowing. It shows not only your knowledge but also your enthusiasm and dedication.
Thanks a lot from the bottom of my heart.

nathans
Автор

everyday is reserved to learn from you ...thanks for this continuation 

rahulsrivastava
Автор

This tutorial is very effective for me, Thank you very much for your support.

deepro__
Автор

thank you i learned a lot from you, keep continue !!

badrslaoui
Автор

I am grateful that your videos are so easy to follow and explain complex subjects understandably. Thanks!

finallyFreeee
Автор

Hi Venkat! You're the best teacher I have never had !

kamdemkakengne
Автор

Thanks for another excellent tutorial.

Ayubajbnabi
Автор

Thank you so much, very helpful . you're amazing buddy!

masoudasadzadeh
Автор

thank you so much for great article,
Can we do this with huge amount of data like with more 1 lac of record ?
what will be the performance in this case ??

abdulrehmanansari
Автор

Can you share videos on Biztalk Tutorial? Thank you

harshitachambhare
Автор

Nice tutorials, could you please make videos of dependency injections.

vivek.tiwary
Автор

can you please make videos on WPF videos 

Rembala
Автор

takewhile vs where: TakeWhile stops when the condition is false, Where continues and find all elements matching the condition....

kylekastilahn
visit shbcf.ru