Browse files

Update Ebuy.Mvc/Extensions/CollectionExtensions.cs

Adding Page<IQueryable> extension
  • Loading branch information...
1 parent 491b747 commit d7fb0b3eb0f185179f4f5ed3e6c27678dbccd74b @jchadwick jchadwick committed Nov 6, 2012
Showing with 14 additions and 3 deletions.
  1. +14 −3 Ebuy.Mvc/Extensions/CollectionExtensions.cs
View
17 Ebuy.Mvc/Extensions/CollectionExtensions.cs
@@ -8,15 +8,26 @@ public static class CollectionExtensions
{
public static IEnumerable<T> Page<T>(this IEnumerable<T> source, int pageIndex, int pageSize)
{
+ return Page<T, IEnumerable<T>>(source, pageIndex, pageSize);
+ }
+
+ public static IQueryable<T> Page<T>(this IQueryable<T> source, int pageIndex, int pageSize)
+ {
+ return Page<T, IQueryable<T>>(source, pageIndex, pageSize);
+ }
+
+ private static U Page<T,U>(this U source, int pageIndex, int pageSize)
+ where U : IEnumerable<T>
+ {
Contract.Requires(pageIndex >= 0, "Page index cannot be negative");
Contract.Requires(pageSize >= 0, "Page size cannot be negative");
int skip = pageIndex * pageSize;
-
+
if (skip > 0)
- source = source.Skip(skip);
+ source = (U)source.Skip(skip);
- source = source.Take(pageSize);
+ source = (U)source.Take(pageSize);
return source;
}

0 comments on commit d7fb0b3

Please sign in to comment.