我经常想为IQueryables提供订单,如果稍后指定另一个订单,它应该像二级订单一样。例如,以下内容:
Repository.All.OrderBy(o => o.Name).OrderBy(o => o.SerialNumber) [A]
应该相当于:
Repository.All.OrderBy(o => o.SerialNumber).ThenBy(o => o.Name)
使用LINQ to SQL可以正常工作。但是,在EntityFramework 4中,生成的SQL中的Order-By子句如下所示:
ORDER BY [Project1].[SerialNumber] ASC
它完全忽略了第一个OrderBy语句,它实际上将OrderBy作为一个稳定的排序。那么我不是一个选项,因为排序并不总是在同一个地方定义(例如,在上面的语句[A]
中,OrderBy(o => o.Name)
可以在存储库中定义。扩展到{{1也不是一个好的解决方案,因为它不允许不同的存储库进行不同的排序,消费代码不应该调用一些IQueryable<TModel>
代码,因为这不是它的关注点。
有没有什么好办法迫使Linq让实体尊重多个OrderBy语句?
由于
答案 0 :(得分:3)
我不同意后续的OrderBy
应该等同于ThenBy
。如果是这样的话,就不需要ThenBy
,你永远不能覆盖现有的排序。
我不能说我喜欢它,但是从我的头脑中看,这似乎是下游排序的一个选择:
IQueryable<Item> items = Repository.GetAllWhichMightBeOrderedAlready();
return items is IOrderedEnumerable<Item>
? ((IOrderedQueryable<Item>)items).ThenBy(x => x.SomeProperty)
: items.OrderBy(x => x.SomeProperty);
视情况替换IOrderedEnumerable<T>
。
答案 1 :(得分:0)
好吧,这不是最优雅的解决方案,但我能够以一种似乎工作得很好的方式克服这个问题,尽管我怀疑所有时髦的反射可能会让它变得太慢。我创建了自己的自定义IQueryable类和关联的查询提供程序,它接受ExpressionVisitor并在GetEnumerator和Execute调用上对该访问者调用.Visit()。我的基础存储库类返回一个新的MappedExpressionQuery,并将从DbContext.Set()返回的查询以及产生所需顺序的ExpressionVisitor传递给它。自定义可查询和提供者类:
public class MappedExpressionQuery<T> : IOrderedQueryable<T>
{
private IQueryable<T> baseQuery;
private MappedExpressionQueryProvider<T> provider;
public MappedExpressionQuery(IQueryable<T> query, ExpressionVisitor expressionMap)
{
baseQuery = query;
provider = new MappedExpressionQueryProvider<T>(query.Provider, expressionMap);
}
#region IOrderedQueryable<T> Members
public IEnumerator<T> GetEnumerator()
{
return baseQuery.Provider.CreateQuery<T>(provider.ExpressionMap.Visit(baseQuery.Expression)).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return baseQuery.Provider.CreateQuery(provider.ExpressionMap.Visit(baseQuery.Expression)).GetEnumerator();
}
public Type ElementType
{
get { return baseQuery.ElementType; }
}
public Expression Expression
{
get { return baseQuery.Expression; }
}
public IQueryProvider Provider
{
get { return provider; }
}
#endregion
}
public class MappedExpressionQueryProvider<T> : IQueryProvider
{
public ExpressionVisitor ExpressionMap { get; private set; }
private IQueryProvider baseProvider;
public MappedExpressionQueryProvider(IQueryProvider baseProvider, ExpressionVisitor expressionMap)
{
this.ExpressionMap = expressionMap;
this.baseProvider = baseProvider;
}
#region IQueryProvider Members
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
return new MappedExpressionQuery<TElement>(baseProvider.CreateQuery<TElement>(expression), ExpressionMap);
}
public IQueryable CreateQuery(Expression expression)
{
throw new NotImplementedException();
}
public TResult Execute<TResult>(Expression expression)
{
return baseProvider.Execute<TResult>(ExpressionMap.Visit(expression));
}
public object Execute(Expression expression)
{
return baseProvider.Execute(ExpressionMap.Visit(expression));
}
#endregion
}
当我的自定义ExpressionVisitor类找到OrderBy或ThenBy语句时,它沿着表达式树向下移动,记录每个排序应该存在的正确顺序,直到找到不是Order语句且不能与Order语句交换的语句。然后它在表达式的末尾再次构建所有语句。因此返回OrderBy(A).ThenBy(B).OrderBy(C).OrderBy(D).ThenBy(E)
,并在末尾添加以下附加表达式:.OrderBy(D).ThenBy(E).ThenBy(C).ThenBy(A).ThenBy(B)
。是的,它是多余的,但EntityFramework无论如何都忽略了链中的表达式,我只使用这个QueryProvider和来自DbContext的可查询。这个表达式访问者的代码(我还提到了.ToString()即使在常量上使用也不会转换为SQL这一事实,因此DbContext.Set<T>().Where(o => o.Name == SomeConstant.ToString())
现在可以正常工作):
public abstract class QueryModifier : ExpressionVisitor
{
private bool OrganizedOrdering { get; set; }
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.Name == "ToString" && node.Method.DeclaringType == typeof(object))
{
try
{
//If the object calling ToString is parameterless, invoke the method and convert it into a constant.
return Expression.Constant(Expression.Lambda(node).Compile().DynamicInvoke());
}
catch (InvalidOperationException)
{
throw new InvalidOperationException("ToString() can only be translated into SQL when used on parameterless expressions.");
}
}
else if (IsOrderStatement(node.Method))
{
if (!OrganizedOrdering)
{
OrganizedOrdering = true;
return RearrangeOrderStatements(node);
}
else
return base.VisitMethodCall(node);
}
else if (OrganizedOrdering && !IsOrderCommutative(node.Method))
{
OrganizedOrdering = false;
return base.VisitMethodCall(node);
}
else
{
return base.VisitMethodCall(node);
}
}
private Expression RearrangeOrderStatements(MethodCallExpression node)
{
//List to store (OrderBy expression, position) tuples
List<Tuple<MethodCallExpression, double>> orderByExpressions = new List<Tuple<MethodCallExpression, double>>();
double low = 0;
double high = 1;
MethodCallExpression startNode = node;
Expression lastNode = node.Arguments[0];
//Travel down the chain and store all OrderBy and ThenBy statements found with their relative positions
while (node != null && node.Method.DeclaringType == typeof(System.Linq.Queryable))
{
if (node.Arguments.Count == 0)
break;
if (node.Method.Name.StartsWith("OrderBy"))
{
orderByExpressions.Add(new Tuple<MethodCallExpression, double>(node, low));
low = low + 1;
high = low + 1;
}
else if (node.Method.Name.StartsWith("ThenBy"))
{
double pos = (high - low) * 0.9 + low;
orderByExpressions.Add(new Tuple<MethodCallExpression, double>(node, pos));
high = pos;
}
else if (!IsOrderCommutative(node.Method))
{
break;
}
lastNode = node.Arguments[0];
node = lastNode as MethodCallExpression;
}
lastNode = startNode;
var methods = typeof(Queryable).GetMethods().Where(o => IsOrderStatement(o));
Type queryType = startNode.Arguments[0].Type.GetGenericArguments()[0];
bool firstStatement = true;
foreach (var tuple in orderByExpressions.OrderBy(o => o.Item2))
{
string methodName;
if (firstStatement)
{
methodName = "OrderBy";
firstStatement = false;
}
else
methodName = "ThenBy";
if (tuple.Item1.Method.Name.EndsWith("Descending"))
methodName = methodName + "Descending";
Type orderByTValueType = tuple.Item1.Arguments[1].Type.GetGenericArguments()[0].GetGenericArguments()[1];
if (tuple.Item1.Arguments.Count == 3)
{
var method = methods.Single(o => o.Name == methodName && o.GetParameters().Length == 3)
.MakeGenericMethod(queryType, orderByTValueType);
lastNode = Expression.Call(method, lastNode, tuple.Item1.Arguments[1], tuple.Item1.Arguments[2]);
}
else
{
var method = methods.Single(o => o.Name == methodName && o.GetParameters().Length == 2)
.MakeGenericMethod(queryType, orderByTValueType);
lastNode = Expression.Call(method, lastNode, tuple.Item1.Arguments[1]);
}
}
return Visit(lastNode);
}
/// <summary>
/// Returns true if the given method call expression is commutative with OrderBy statements.
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
private bool IsOrderCommutative(MethodInfo method)
{
return new string[] { "Where", "Distinct", "AsQueryable" }.Contains(method.Name)
&& method.DeclaringType == typeof(System.Linq.Queryable);
}
private bool IsOrderStatement(MethodInfo method)
{
return (method.Name.StartsWith("OrderBy") || method.Name.StartsWith("ThenBy"))
&& method.DeclaringType == typeof(System.Linq.Queryable);
}
}
答案 2 :(得分:0)
所以你不能使用ThenBy,因为可能会跳过初始的OrderBy?如何做一个初始的虚拟OrderBy,然后所有其他的都是ThenBy。
// Basically, everything gets the same orderby ranking
// I don't know offhand if you can use a constant here, but if you have an id,
// you should be able to this.
var list = context.MyTable.OrderBy(mt => mt.id - mt.id);
if (order by field1)
list = list.ThenBy(mt => mt.field1);
if (order by field2)
list = list.ThenBy(mt => mt.field2);
等...
编辑:没关系。这不起作用。正如我所想的那样,不能单独使用ThenBy独立的行。