I found Marc Gravell’s dynamic order by great:
Dynamic LINQ OrderBy on IEnumerable<T>
I’ve put it in a class, LinqHelper. In this class I also have created two new classes, so that in my code I can do this:
var q = db.tblJobHeaders;
LinqHelper.OrderByCollection OBys = new LinqHelper.OrderByCollection();
OBys.AddOrderBy("some field", true);
OBys.AddOrderBy("anotherfield", false);
OBys.ExecuteOrderBys(q);
The classes to acheive this are:
/// <summary>
/// A collection of order bys
/// </summary>
public class OrderByCollection
{
private ArrayList Orderings = new ArrayList();
public OrderByCollection(){ }
/// <summary>
/// Add an order by to this collection
/// </summary>
public void AddOrderBy(string Field, bool Descending)
{
OrderByObj NewObj = new OrderByObj(Descending, Field);
this.Orderings.Add(NewObj);
}
/// <summary>
/// Executes the order bys
/// </summary>
public IOrderedQueryable<T> ExecuteOrderBys<T>(this IOrderedQueryable<T> source)
{
int ExecutionIndex = 0;
foreach (OrderByObj O in this.Orderings)
{
if (ExecutionIndex == 0)
{
if (O.Descending)
source = LinqHelper.OrderByDescending(source, O.Field);
else
source = LinqHelper.OrderBy(source, O.Field);
}
else
{
if (O.Descending)
source = LinqHelper.ThenByDescending(source, O.Field);
else
source = LinqHelper.ThenBy(source, O.Field);
}
ExecutionIndex++;
}
return (IOrderedQueryable<T>)source;
}
}
/// <summary>
/// An order by object
/// </summary>
private class OrderByObj
{
public bool Descending { get; set; }
public string Field { get; set; }
public OrderByObj(bool IsDescending, string DatabaseField)
{
this.Descending = IsDescending;
this.Field = DatabaseField;
}
}
Howver I’m pretty new to passing Linq vars through to functions (the confuses me a bit). I currently get the error on:
OBys.ExecuteOrderBys(q);
Which gives the error:
The type arguments for method
‘LinqHelper.OrderByCollection.ExecuteOrderBys(System.Linq.IOrderedQueryable)’
cannot be inferred from the usage. Try
specifying the type arguments
explicitly.
I’m a bit confused about this if anyone could help, am I passing the var q in properly, and then returning it properly?
I bet the type of
qisIQueryable<T>and notIOrderedQueryable<T>. Just changing the signature should work, because you start withOrderBy.Then you will need an
IOrderedQueryable<T>for theThenBys. You can just cast it, because you know for sure that you have anIOrderedQueryable<T>from the previous call to eitherOrderByorThenBy.If you don’t like the idea of the cast, you need some changes:
Note your code will fail spectacularly if you don’t add any orderings, because of the cast to
IOrderedQueryable<T>in the end. You could change the return type toIQueryable<T>(which loses the ability to “attach” more OrderBys later), or throw if there are no orderings, like I did.