Barış Kısır

Senior Software Developer

Navigation
 » Home
 » RSS

Paging with LINQ

21 Jan 2017 » csharp

How do you paging?

We’ll use LINQ’s Take and Skip Methods

I’ll use test json data, so install Newtonsoft

Installation with NuGet

Install-Package Newtonsoft.Json -Version 9.0.1

Lets create class for our test data

 public class RootObject
{
	public int userId { get; set; }
	public int id { get; set; }
	public string title { get; set; }
	public string body { get; set; }
}

How we parse json

private List<RootObject> GetJsonFromUrl(String url)
{
	using (WebClient wc = new WebClient())
	{
		String json = wc.DownloadString(url);
		List<RootObject> rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RootObject>>(json);
		return rootObject;
	}
}

How we paging

private List<RootObject> MakePaging(Int32 numberOfItem, Int32 pageNumber, String url)
{
	List<RootObject> result = GetJsonFromUrl(url)
		.Skip(numberOfItem * (pageNumber - 1))
		.Take(numberOfItem).ToList();
	return result;
}

You can download source code from here –> Download

validation-1

validation-1

validation-1

validation-1