Ejemplo n.º 1
0
 public PagedResult(PagingCriteria paging, int total, IEnumerable <T> data)
 {
     Page       = paging.Page;
     Size       = paging.Size;
     TotalItems = total;
     TotalPages = total <= 0 ? 0 : total <= paging.Size ? 1 : (total / paging.Size);
     Data       = data as T[] ?? data.ToArray();
 }
Ejemplo n.º 2
0
 public static PagedResult <T> Empty(PagingCriteria paging)
 {
     return(new PagedResult <T>()
     {
         Page = paging.Page,
         Size = paging.Size,
         TotalItems = 0,
         TotalPages = 0
     });
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Search for tasks matching the given criteria and return a paged list of results.
        /// </summary>
        /// <param name="criteria"></param>
        /// <param name="paging"></param>
        /// <returns></returns>
        public async Task <PagedResult <SubtaskSearchResult> > Find(SubtaskSearchCriteria criteria, PagingCriteria paging)
        {
            if (paging.Page <= 0 || paging.Size <= 0)
            {
                return(PagedResult <SubtaskSearchResult> .Empty(paging));
            }

            // Build search query
            var query = _dbContext.Tasks.AsQueryable();

            if (!string.IsNullOrEmpty(criteria.Name))
            {
                query = query.Where(j => j.Name == criteria.Name);
            }

            if (criteria.JobId.HasValue)
            {
                query = query.Where(j => j.Job.JobId == criteria.JobId);
            }

            var pageSize = paging.Size;
            var skipRows = (paging.Page - 1) * paging.Size;

            var page = await query
                       .OrderBy(t => t.Id)
                       .Select(t => new
            {
                Job = new SubtaskSearchResult
                {
                    JobId  = t.Job.JobId,
                    TaskId = t.TaskId,
                    Name   = t.Name,
                    Status = t.Status
                },
                TotalCount = query.Count()     // note: EF optimizes this so it's only executed once
            })
                       .Skip(skipRows).Take(pageSize)
                       .ToArrayAsync();

            // No results found, return an empty result set
            if (page == null || page.Length == 0)
            {
                return(PagedResult <SubtaskSearchResult> .Empty(paging));
            }

            // More than one row found!
            // Build the paged result object
            int total = page.First().TotalCount;
            var jobs  = page.Select(p => p.Job);

            return(new PagedResult <SubtaskSearchResult>(paging, total, jobs));
        }