Home > Development > C# PagedList With Iterator Support

C# PagedList With Iterator Support


Hi All,
If you need a class To hold your paged data and also contain the maximum number of nows available with your search criteria. I have some code to share with you I hope you will find useful.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace CSharpIterators
{

    public class PagedList<T> : IEnumerable<T>
    {

        private readonly IEnumerable<T> _items;
        private readonly int _totalitems;

        public PagedList(IEnumerable<T> items, int totalitems)
        {
            this._items = items;
            this._totalitems = totalitems;
        }

        public IEnumerable<T> Items
        {
            get { return _items; }
        }

        public int TotalItems
        {
            get { return _totalitems; }
        }

        public IEnumerator<T> GetEnumerator()
        {
            if(Items == null || Items.Count() == 0)
                yield break;

            foreach (var item in Items)
            {
                yield return item;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public static class PagedListFactory
    {
        public static PagedList<T> GetPagedListFor<T>(IEnumerable<T> items, int totalitems)
        {
            return new PagedList<T>(items,totalitems);
        }
    }

    public class Person
    {
        private string _name;

        public Person(string name)
        {
            this._name = name;
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    class Program
    {
        static void Main()
        {

            List<Person> people = new List<Person>
                                      {
                                          new Person("Blair"),
                                          new Person("Tim"),
                                          new Person("Esther")
                                      };

            IEnumerable<Person> items = PagedListFactory.GetPagedListFor(people, 10);

            foreach (var person in items)
                Console.WriteLine(person.Name);

            items = PagedListFactory.GetPagedListFor<Person>(null, 10);

            foreach (var person in items)
                Console.WriteLine(person.Name);

            Console.ReadKey();
        }
    }
}

Blair

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: