I found the dynamic order of Mark Gravell great:
Dynamic LINQ OrderBy on IEnumerable <T>
I put it in a class, LinqHelper
. In this class, I also created two new classes, so 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);
Classes for this:
/// <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; } }
No matter how new I am to passing Linq vars through functions (it bothers me a bit). I am currently getting the error message:
OBys.ExecuteOrderBys(q);
Which gives an error:
Type arguments for the 'LinqHelper.OrderByCollection.ExecuteOrderBys (System.Linq.IOrderedQueryable)' method cannot be taken out of use. Try specifying type arguments explicitly.
I'm a little confused by this, if anyone can help, am I passing var q
correctly and then returning it correctly?
Tom gullen
source share