Back to Browse

Part 13 Implement paging using skip and take operators

68.1K views
Jul 16, 2014
9:54

Text version of the video http://csharp-video-tutorials.blogspot.com/2014/07/part-13-implement-paging-using-skip-and.html 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. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 Slides http://csharp-video-tutorials.blogspot.com/2014/07/part-13-implement-paging-using-skip-and_16.html LINQ Tutorial - All Text Articles & Slides http://csharp-video-tutorials.blogspot.com/2014/07/linq-tutorial.html LINQ Tutorial Playlist https://www.youtube.com/playlist?list=PL6n9fhu94yhWi8K02Eqxp3Xyh_OmQ0Rp6 Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd 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.

Download

0 formats

No download links available.

Part 13 Implement paging using skip and take operators | NatokHD