I need to know if an OrderBy was applied to a Linq query before applying an .Skip or .Take. I have no control of the received query and if an OrderBy was applied I need to maintain this one, in any other case I should OrderBy(t=>true). I have tried the following:
DataContext db;
var query = db.Orders;
var wasOrderByApplied = typeof(IOrderedQueryable<Order>).IsAssignableFrom(query.AsQueryable().Expression.Type);
var wasOrderByApplied2 = query.AsQueryable().Expression.Type == typeof(System.Data.Entity.Core.Objects.ObjectQuery<Order>);
var wasOrderByApplied3 = typeof(IOrderedQueryable<Order>) == query.AsQueryable().Expression.Type;
var query2 = db.Orders.OrderBy(o => o.CreationDate);
var wasOrderByApplied4 = typeof(IOrderedQueryable<Order>).IsAssignableFrom(query2.AsQueryable().Expression.Type);
var wasOrderByApplied5 = query2.AsQueryable().Expression.Type == typeof(System.Data.Entity.Core.Objects.ObjectQuery<Order>);
var wasOrderByApplied6 = typeof(IOrderedQueryable<Order>) == query2.AsQueryable().Expression.Type;
var query3 = db.Orders.OrderBy(o => o.CreationDate).Where(o => o.Id > 4);
var wasOrderByApplied7 = typeof(IOrderedQueryable<Order>).IsAssignableFrom(query3.AsQueryable().Expression.Type);
var wasOrderByApplied8 = query3.AsQueryable().Expression.Type == typeof(System.Data.Entity.Core.Objects.ObjectQuery<Order>);
var wasOrderByApplied9 = typeof(IOrderedQueryable<Order>) == query3.AsQueryable().Expression.Type;
The results where:
wasOrderByApplied = true;
wasOrderByApplied2 = true;
wasOrderByApplied3 = false;
wasOrderByApplied4 = true;
wasOrderByApplied5 = false;
wasOrderByApplied6 = true;
With the last results it seems that the third question asked to each query is the one that is correct, but then I did the third query (query3) and the results where:
wasOrderByApplied7 = false;
wasOrderByApplied8 = false;
wasOrderByApplied9 = false;
When I add a Where after the OrderBy the question result is false where it should be true.
Is there a better way to know if an OrderBy was applied to the query?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…