Monday, 7 November 2011

c#: Page a list

So today i wanted a noob way to code a pager function. So here is what i came up with, Pretty noob but works:

        public int PageSize
        {
            get {
                return this.Tiles.Count / 5;
            }
        }

        public List<T> GetPage<T>(int PageNumber)
        {
            List<T> results = new List<T>();
            for (int i = 1; i <= this.Tiles.Count; i++)
            {
                    if (((i / PageSize) % 5 == PageNumber))
                    {
                        results.Add(this.Tiles[i]);
                    }
            }                
            return results;
        }




yep, that's it.. the Page size is divided by 5, so if you had a list of 100 items, you'd get around 20 items!

stay tuned for more noob coding!

No comments:

Post a Comment