I need to merge these two methods in only one generalizing them. I know IQueryable implements IEnumerable, IOrderedQueryable implements IOrderedEnumerable and the first method seems to be useless if the second is present. But for some Entity Framework reasons the second one breaks the server-side query translating (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);
}
}
These methods break the DRY rule and it's awfully annoying.