如何知道是否应用于排序依据查询? [英] How to know if OrderBy was applied to query?
问题描述
我需要知道,如果排序依据是施加.Skip或之前。取应用到LINQ查询。我没有接收到查询的控制,如果被施加的OrderBy我需要保持这一个,在其他任何情况下,我应该排序依据(T => TRUE)。我曾尝试以下内容:
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;
随着最后的结果似乎是要求每个查询的第三个问题是,一个是正确的,但后来我做了第三个查询(QUERY3),结果其中:
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?
推荐答案
下面是来到我的心中只有一个可能的解决方案:
Here is one possible solution that came to my mind:
创建表达式游客支票该表达式是否调用排序依据
,或者类似这样的 OrderByDescending
方法:
Create an expression visitor that checks whether the expression invokes the OrderBy
, or the OrderByDescending
method like this:
public class MyVisitor : ExpressionVisitor
{
public bool HasOrderBy { get; private set; }
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.DeclaringType == typeof (Queryable) &&
(node.Method.Name == "OrderBy" || node.Method.Name == "OrderByDescending"))
HasOrderBy = true;
return base.VisitMethodCall(node);
}
}
下面是如何使用它:
MyVisitor visitor = new MyVisitor();
visitor.Visit(query.Expression);
if (visitor.HasOrderBy)
{
//..
}
else
{
//..
}
这篇关于如何知道是否应用于排序依据查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!