for
环基本上通过所有的
Order
值,使用
OrderBy
对于第一个,以及
ThenBy
IQueryable
,我已将其修改为使用临时
IOrderedQueryable<T>
变量
if (spec.Order != null)
{
var count = spec.Order.Count;
IOrderedQueryable<T> orderedQuery = null;
for (int i = 0; i < count; ++i)
{
if (i == 0)
{
orderedQuery = query.OrderBy(spec.Order[i]);
}
else
{
orderedQuery = orderedQuery.ThenBy(spec.Order[i]);
}
}
query = orderedQuery ?? query;
}
您也可以这样处理,尽管我不确定这两种方法之间的性能有何差异,如果它确实存在的话:
if (spec.Order != null)
{
var count = spec.Order.Count;
for (int i = 0; i < count; ++i)
{
if (query is IOrderedQueryable<T> orderedQuery)
{
query = orderedQuery.ThenBy(spec.Order[i]);
}
else
{
query = query.OrderBy(spec.Order[i]);
}
}
}