Created
November 23, 2012 07:51
-
-
Save yicone/4134422 to your computer and use it in GitHub Desktop.
Do sort and pager for both IQueryable<T> and IEnumerable<T> meanwhile.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Foo | |
| { | |
| public virtual IEnumerable<Product> Page(IEnumerable<Product> list, Model.Pager pager) | |
| { | |
| IEnumerable<Product> b; | |
| if (pager == null) | |
| { | |
| throw new ArgumentNullException("pager"); | |
| } | |
| var q = list as IQueryable<Product>; | |
| // 排序 | |
| if (pager.SortRule == Model.SortRule.ProductDefault) | |
| { | |
| if (q != null) | |
| { | |
| q = q.OrderByDescending(p => p.Weight) | |
| .ThenBy(p => p.EffectDate) | |
| .ThenBy(p => p.TripDays); | |
| } | |
| else | |
| { | |
| list = list.OrderByDescending(p => p.Weight) | |
| .ThenBy(p => p.EffectDate) | |
| .ThenBy(p => p.TripDays); | |
| } | |
| } | |
| else | |
| { | |
| if (pager.SortRule == Model.SortRule.ProductTripDays) | |
| { | |
| if (q != null) | |
| { | |
| q = q.OrderBy(p => p.TripDays); | |
| } | |
| else | |
| { | |
| list.OrderBy(p => p.TripDays); | |
| } | |
| } | |
| else if (pager.SortRule == Model.SortRule.ProductMinDepartureDate) | |
| { | |
| if (q != null) | |
| { | |
| q = q.OrderBy(p => p.EffectDate); | |
| } | |
| else | |
| { | |
| list.OrderBy(p => p.EffectDate); | |
| } | |
| } | |
| else if (pager.SortRule == Model.SortRule.ProductMinPrice) | |
| { | |
| if (q != null) | |
| { | |
| q = q.OrderBy(p => p.Product_Price.Min(pr => pr.Price)); | |
| } | |
| else | |
| { | |
| list.OrderBy(p => p.Product_Price.Min(pr => pr.Price)); | |
| } | |
| } | |
| else | |
| { | |
| throw new ArgumentOutOfRangeException("pager.SortRule"); | |
| } | |
| if (pager.Descending) | |
| { | |
| if (q != null) | |
| { | |
| q = q.Reverse(); | |
| } | |
| else | |
| { | |
| list = list.Reverse(); | |
| } | |
| } | |
| } | |
| int pageCount; | |
| if (q != null) | |
| { | |
| b = PagerHelper.TakePage(q, pager.PageSize, pager.PageIndex, out pageCount); | |
| } | |
| else | |
| { | |
| b = PagerHelper.TakePage(list, pager.PageSize, pager.PageIndex, out pageCount); | |
| } | |
| pager.PageCount = pageCount; | |
| return b; | |
| } | |
| } | |
| public class PagerHelper | |
| { | |
| public static IEnumerable<T> TakePage<T>(IEnumerable<T> list, int pageSize, int? pageIndex, out int pageCount) where T : class | |
| { | |
| int count = list.Count(); | |
| pageCount = (int)Math.Ceiling((decimal)count / pageSize); | |
| int i = 0; | |
| if (pageIndex.HasValue) | |
| { | |
| i = pageIndex.Value; | |
| // fix range | |
| if (i < 0) i = 0; | |
| } | |
| // fix range | |
| if (pageIndex > pageCount) | |
| pageIndex = pageCount; | |
| int skip = i * pageSize; | |
| int take = pageSize; | |
| var q = list as IQueryable<T>; | |
| if (q != null) | |
| { | |
| return q.Skip(skip).Take(take); | |
| } | |
| else | |
| { | |
| return list.Skip(skip).Take(take); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment