受this的启发我做了这个:
ISortable
public interface ISortable<T>
{
IPageable<T> OrderBy<U>(Expression<Func<T, U>> orderBy);
IPageable<T> OrderByDescending<U>(Expression<Func<T, U>> orderBy);
}
IPageable
public interface IPageable<T> : ISortable<T>, IEnumerable<T>
{
IPageable<T> Page(int pageNumber, int pageSize);
List<T> ToList();
int TotalPages { get; }
int TotalItemCount { get; }
int PageNumber { get; }
int? PageSize { get; }
}
可分页
public class Pageable<T> : IPageable<T>
{
private readonly IQueryable<T> _countQuery;
private IQueryable<T> _sourceQuery;
/// <summary>
/// A pageable result
/// </summary>
/// <param name="sourceQuery">Query which holdes all relevant items.</param>
public Pageable(IQueryable<T> sourceQuery)
{
_sourceQuery = sourceQuery;
_countQuery = sourceQuery;
PageNumber = 1;
}
/// <summary>
/// A pageable result
/// </summary>
/// <param name="sourceQuery">Query which holdes all relevant items.</param>
/// <param name="countQuery">
/// Alternative query optimized for counting.
/// <see cref="countQuery"/> is required to give the same count as <see cref="sourceQuery"/> else paging will break.
/// <remarks>No checks if <see cref="sourceQuery"/> and <see cref="countQuery"/> return the same count are appiled.</remarks>
/// </param>
public Pageable(IQueryable<T> sourceQuery, IQueryable<T> countQuery)
: this (sourceQuery)
{
_countQuery = countQuery;
}
#region Implementation of IEnumerable
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<T> GetEnumerator()
{
return _sourceQuery.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Implementation of ISortable
public IPageable<T> OrderBy<U>(Expression<Func<T, U>> orderBy)
{
_sourceQuery = _sourceQuery.OrderBy(orderBy);
return this;
}
public IPageable<T> OrderByDescending<U>(Expression<Func<T, U>> orderBy)
{
_sourceQuery = _sourceQuery.OrderByDescending(orderBy);
return this;
}
#endregion
#region Implementation of IPageable
public int PageNumber { get; private set; }
public int? PageSize { get; private set; }
public int TotalItemCount
{
get { return _countQuery.Count(); }
}
public int TotalPages
{
get { return (int) (Math.Ceiling((double) TotalItemCount/PageSize ?? 1)); }
}
/// <summary>
/// Chop a query result into pages.
/// </summary>
/// <param name="pageNumber">Page number to fetch. Starting from 1.</param>
/// <param name="pageSize">Items per page.</param>
/// <returns></returns>
public IPageable<T> Page(int pageNumber, int pageSize)
{
PageNumber = pageNumber;
PageSize = pageSize;
_sourceQuery = _sourceQuery
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize);
return this;
}
public List<T> ToList()
{
return _sourceQuery.ToList();
}
#endregion
}
以上作品。巨大的成功! :)
但是我遇到了实现方法.ThenBy()
的问题。问题是只有在.ThenBy()
被调用时才能访问.OrderBy()
。
我注意到IQueryable.OrderBy返回了一个IOrderedQueryable,而.ThenBy()
的访问权来自于{{1}}。但是为了使我当前的解决方案工作,我需要制作一个IOrderedPageable和一个新的OrderedPagable。 OrderedPagable几乎就是Pageable的副本,这真的是非常糟糕的设计。
我非常怀疑它是如何在LINQ中完成的。所以我的问题是,他们是怎么做到的?我很好奇:)
我注意到的一件事是,几乎所有的LINQ方法都是扩展方法,是“技巧”的一部分:)?
答案 0 :(得分:6)
听起来您的班级OrderedPageable
可能是Pageable
的子类,另外还会实现IOrderedPageable
界面。
继承方法似乎有意义,因为处理Pageable
的任何内容都应该能够以相同的方式处理OrderedPageable
。
通过稍微更改接口并使用类似于上面的接口继承方法,您可以使用不可变的可查询类和扩展方法来实现您正在寻找的功能。
在我看来,用法更清晰,更符合LINQ。
示例:
query.AsPageable(100).Page(1);
query.AsPageable(100).OrderBy(x => x.Name).ThenBy(x => x.Age).Page(1).ToList();
我没有测试过这个,但这个概念应该有效。请注意:
SourceQuery
IPageableQuery
属性才能在PageableExtensions
中使用,因为您的查询提供程序可能无法成功翻译针对此新PageableQuery
的查询1}}类型。接口:
public interface IPageableQuery<T> : IQueryable<T>
{
int TotalPages { get; }
int TotalItemCount { get; }
int PageSize { get; }
}
public interface IOrderedPageableQuery<T> : IPageableQuery<T>, IOrderedQueryable<T>
{
}
实现:
public class PageableQuery<T> : IPageableQuery<T>
{
readonly IQueryable<T> _sourceQuery;
readonly Lazy<int> _totalItemCount;
public int TotalPages { get { return (int)Math.Ceiling((double)TotalItemCount / PageSize); } }
public int TotalItemCount { get { return _totalItemCount.Value; } }
public int PageSize { get; private set; }
public PageableQuery(IQueryable<T> sourceQuery, int pageSize)
{
_sourceQuery = sourceQuery;
_totalItemCount = new Lazy<int>(() => _sourceQuery.Count());
PageSize = pageSize;
}
public IEnumerator<T> GetEnumerator() { return _sourceQuery.GetEnumerator();}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public Expression Expression { get { return _sourceQuery.Expression; } }
public Type ElementType { get { return _sourceQuery.ElementType; } }
public IQueryProvider Provider { get { return _sourceQuery.Provider; } }
}
public class OrderedPageableQuery<T> : IOrderedPageableQuery<T>
{
readonly IPageableQuery<T> _sourcePageableQuery;
readonly IOrderedQueryable<T> _sourceQuery;
public int TotalPages { get { return (int)Math.Ceiling((double)TotalItemCount / PageSize); } }
public int TotalItemCount { get { return _sourcePageableQuery.TotalItemCount; } }
public int PageSize { get { return _sourcePageableQuery.PageSize; } }
public OrderedPageableQuery(IPageableQuery<T> sourcePageableQuery, IOrderedQueryable<T> newSourceQuery)
{
_sourcePageableQuery = sourcePageableQuery;
_sourceQuery = newSourceQuery;
}
public IEnumerator<T> GetEnumerator() { return _sourceQuery.GetEnumerator();}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public Expression Expression { get { return _sourceQuery.Expression; } }
public Type ElementType { get { return _sourceQuery.ElementType; } }
public IQueryProvider Provider { get { return _sourceQuery.Provider; } }
}
扩展方法:
public static class PageableExtension
{
public static IPageableQuery<T> AsPageable<T>(this IQueryable<T> sourceQuery, int pageSize)
{
return new PageableQuery<T>(sourceQuery, pageSize);
}
public static IOrderedPageableQuery<T> OrderBy<T, U>(this IPageableQuery<T> sourcePageableQuery, Expression<Func<T, U>> orderBy)
{
return new OrderedPageableQuery<T>(sourcePageableQuery, Queryable.OrderBy(sourcePageableQuery, orderBy));
}
public static IOrderedPageableQuery<T> OrderByDescending<T, U>(this IPageableQuery<T> sourcePageableQuery, Expression<Func<T, U>> orderBy)
{
return new OrderedPageableQuery<T>(sourcePageableQuery, Queryable.OrderByDescending(sourcePageableQuery, orderBy));
}
public static IOrderedPageableQuery<T> ThenBy<T, U>(this IOrderedPageableQuery<T> sourcePageableQuery, Expression<Func<T, U>> orderBy)
{
return new OrderedPageableQuery<T>(sourcePageableQuery, Queryable.ThenBy(sourcePageableQuery, orderBy));
}
public static IOrderedPageableQuery<T> ThenByDescending<T, U>(this IOrderedPageableQuery<T> sourcePageableQuery, Expression<Func<T, U>> orderBy)
{
return new OrderedPageableQuery<T>(sourcePageableQuery, Queryable.ThenByDescending(sourcePageableQuery, orderBy));
}
public static IQueryable<T> Page<T>(this IPageableQuery<T> sourceQuery, int pageNumber)
{
return sourceQuery.Skip((pageNumber - 1) * sourceQuery.PageSize)
.Take(sourceQuery.PageSize);
}
}
答案 1 :(得分:2)
我建议的设计方法是明确表示你的方法实际上是在改变源对象,故意不反映LINQ方法名称以避免混淆。为了清楚起见,我省略了IPageable
接口和一堆其他东西,因为代码已经有点冗长了:
public interface ISortable<T>
{
Pageable<T> ResetOrder();
Pageable<T> AddOrder(Expression<Func<T, object>> orderBy);
Pageable<T> AddOrderDescending(Expression<Func<T, object>> orderBy);
}
public class Pageable<T> : ISortable<T>, IEnumerable<T> {
class SortKey {
public Expression<Func<T, object>> Expression { get; set; }
public bool Descending { get; set; }
}
List<SortKey> _sortKeys = new List<SortKey>();
System.Linq.IQueryable<T> _sourceQuery;
int _pageNumber;
int _pageSize;
public Pageable<T> SetPage(int pageNumber, int pageSize) {
_pageNumber = pageNumber;
_pageSize = pageSize;
return this;
}
public Pageable<T> ResetOrder()
{
_sortKeys.Clear();
return this;
}
public Pageable<T> AddOrder(Expression<Func<T, object>> orderBy)
{
_sortKeys.Add(new SortKey {
Expression = orderBy,
Descending = false
});
return this;
}
public Pageable<T> AddOrderDescending(Expression<Func<T, object>> orderBy)
{
_sortKeys.Add(new SortKey {
Expression = orderBy,
Descending = true
});
return this;
}
IEnumerable<T> SortAndPage()
{
if (_sortKeys.Count == 0)
{
return Page(_sourceQuery);
}
var firstKey = _sortKeys[0];
var orderedQuery = firstKey.Descending
? _sourceQuery.OrderByDescending(firstKey.Expression)
: _sourceQuery.OrderBy(firstKey.Expression);
foreach (var key in _sortKeys.Skip(1))
{
orderedQuery = key.Descending ? orderedQuery.ThenByDescending(key.Expression) : orderedQuery.ThenBy(key.Expression);
}
return Page(orderedQuery);
}
IEnumerable<T> Page(IQueryable<T> query)
{
return query.Skip((_pageNumber - 1) * _pageSize)
.Take (_pageSize);
}
public IEnumerator<T> GetEnumerator()
{
return SortAndPage().GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
请注意,源查询保持不变,因此您可以在枚举元素之前更改参数。如果您希望在执行此操作的方法中基于当前的Pageable
而不是this
返回新的SortAndPage()
,也可以实现链接行为,但这会使代码更加笨拙,因为您' d创建一个复制构造函数并添加代码来创建那些派生对象。
另外我相信{{1}}中烦人的冗余代码可以使用一些FP方法进行重构(或者至少是高尔夫球化)但是阅读它的方式可能更直接。