I have used these extension methods to achieve something similar:
public static string GetKeyField(Type type)
{
var allProperties = type.GetProperties();
var keyProperty = allProperties.SingleOrDefault(p => p.IsDefined(typeof(KeyAttribute)));
return keyProperty != null ? keyProperty.Name : null;
}
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string orderBy)
{
return source.GetOrderByQuery(orderBy, "OrderBy");
}
public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string orderBy)
{
return source.GetOrderByQuery(orderBy, "OrderByDescending");
}
private static IQueryable<T> GetOrderByQuery<T>(this IQueryable<T> source, string orderBy, string methodName)
{
var sourceType = typeof(T);
var property = sourceType.GetProperty(orderBy);
var parameterExpression = Expression.Parameter(sourceType, "x");
var getPropertyExpression = Expression.MakeMemberAccess(parameterExpression, property);
var orderByExpression = Expression.Lambda(getPropertyExpression, parameterExpression);
var resultExpression = Expression.Call(typeof(Queryable), methodName,
new[] { sourceType, property.PropertyType }, source.Expression,
orderByExpression);
return source.Provider.CreateQuery<T>(resultExpression);
}
This allows you to pass the property name as a string and build up an expression which it passes to the regular LINQ OrderBy() function. So in your case, the usage would be:
DbSet = Context.Set<T>();
public IQueryable<T> GetAll(int pageNumber = 0, int pageSize = 10, string sortColumn = "")
{
return DbSet.OrderBy(GetKeyField(typeof(T))).Skip(pageNumber * pageSize)Take(pageSize);
}
This assumes your key field in your entity class is properly decorated with the Key
attribute.
Skip
thenTake
– Manda