我需要将这两种方法合并为一种概括它们的方法。我知道 IQueryable 实现了 IEnumerable,IOrderedQueryable 实现了 IOrderedEnumerable,如果存在第二种方法,第一种方法似乎没用。但是由于某些实体框架的原因,第二个破坏了服务器端查询转换(Linq-to-SQL)
private static IQueryable<T> Pag<T>(IOrderedQueryable<T> data, int totalRows, int reqLength, int pageNum)
{
if (reqLength == 0)
return data;
else
{
int skipRows = reqLength * (pageNum - 1);
if (skipRows >= totalRows)
skipRows = 0;
int diff = totalRows - skipRows;
return data.Skip(skipRows).Take(diff > reqLength ? reqLength : diff);
}
}
private static IEnumerable<T> Pag<T>(IOrderedEnumerable<T> data, int totalRows, int reqLength, int pageNum)
{
if (reqLength == 0)
return data;
else
{
int skipRows = reqLength * (pageNum - 1);
if (skipRows >= totalRows)
skipRows = 0;
int diff = totalRows - skipRows;
return data.Skip(skipRows).Take(diff > reqLength ? reqLength : diff);
}
}
这些方法违反了DRY规则,非常烦人。