コード例 #1
0
        public async Task <List <T> > GetPageAsync <TKey>(int PageNumeber, int PageSize, Expression <Func <T, bool> > filter, Expression <Func <T, TKey> > sortingExpression, SortDirection sortDir = SortDirection.Ascending, string includeProperties = "")
        {
            IQueryable <T> query     = context.Set <T>();
            int            skipCount = (PageNumeber - 1) * PageSize;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            query = includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Aggregate(query, (current, includeProperty) => current.Include(includeProperty));

            switch (sortDir)
            {
            case SortDirection.Ascending:
                if (skipCount == 0)
                {
                    query = query.OrderBy <T, TKey>(sortingExpression).Take(PageSize);
                }
                else
                {
                    query = query.OrderBy <T, TKey>(sortingExpression).Skip(skipCount).Take(PageSize);
                }
                break;

            case SortDirection.Descending:
                if (skipCount == 0)
                {
                    query = query.OrderByDescending <T, TKey>(sortingExpression).Take(PageSize);
                }
                else
                {
                    query = query.OrderByDescending <T, TKey>(sortingExpression).Skip(skipCount).Take(PageSize);
                }
                break;

            default:
                break;
            }
            return(await query.AsNoTracking().ToListAsync());
        }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Repository{T}"/> class.
 /// </summary>
 /// <param name="context">The context<see cref="GBSampleContext"/></param>
 public Repository(GBSampleContext context)
 {
     this.context = context;
     entities     = context.Set <T>();
 }
コード例 #3
0
 public AuthorRepository(GBSampleContext context) : base(context)
 {
     entity = context.Set <Author>();
 }
コード例 #4
0
 public ReviewRepository(GBSampleContext context) : base(context)
 {
     entity = context.Set <Review>();
 }
コード例 #5
0
 public BookRepository(GBSampleContext context) : base(context)
 {
     entity = context.Set <Book>();
 }