using System.Collections.Generic;
using System.Linq.Expressions;
public static void Main()
Console.WriteLine("Hello World");
var myList = new List<MyClass>()
new MyClass(1, "C", MyType.Primary),
new MyClass(2, "B", MyType.Secondary),
new MyClass(3, "A", MyType.Primary),
myList = myList.GetPage(0, 4, "Type", "asc");
foreach(var item in myList)
Console.WriteLine("--- Item ---");
Console.WriteLine("Id = " + item.Id);
Console.WriteLine("Name = " + item.Name);
Console.WriteLine("Type = " + item.Type);
public static class ListExtensions
public static List<T> GetPage<T>(this List<T> source, int currentPage, int pageSize, string orderBy, string orderDirection)
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter, orderBy);
var converted = Expression.Convert(property, typeof(object));
var orderByExpression = Expression.Lambda<Func<T, object>>(converted, parameter);
IOrderedQueryable<T> sourceOrdered;
switch (orderDirection.ToLower())
sourceOrdered = source.AsQueryable<T>().OrderByDescending<T, object>(orderByExpression);
sourceOrdered = source.AsQueryable<T>().OrderBy<T, object>(orderByExpression);
return sourceOrdered.Skip(pageSize * currentPage).Take(pageSize).ToList();
public int Id { get; set; }
public string Name { get; set; }
public MyType Type { get; set; }
public MyClass(int id, string name, MyType type)