Skip to content

Commit e873f5e

Browse files
committed
Add GetAllPagingAsync method to IRepository and Repository
Updated the `IRepository<TEntity, TKey>` interface to include a new method `GetAllPagingAsync` for asynchronous retrieval of paginated results with optional filtering, ordering, and related entity inclusion. Implemented the `GetAllPagingAsync` method in the `Repository<TEntity, TKey>` class, allowing for flexible querying and pagination. Added XML documentation comments for clarity on method functionality and parameters.
1 parent 09e74e3 commit e873f5e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/ClassLibrary.EFCore/Interfaces/IRepository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,22 @@ Task<IQueryable<TEntity>> GetAllAsync(Func<IQueryable<TEntity>,
5757
/// <param name="pageSize">The size of the page.</param>
5858
/// <returns>A task that represents the asynchronous operation. The task result contains a PaginatedResult of TEntity.</returns>
5959
Task<PaginatedResult<TEntity>> GetPaginatedAsync(IQueryable<TEntity> query, int pageNumber, int pageSize);
60+
61+
/// <summary>
62+
/// Retrieves a paginated result of entities asynchronously with optional filtering, ordering, and including related entities.
63+
/// </summary>
64+
/// <param name="pageNumber">The page number to retrieve.</param>
65+
/// <param name="pageSize">The size of the page.</param>
66+
/// <param name="includes">A function to include related entities in the query.</param>
67+
/// <param name="filter">A filter expression to apply to the entities.</param>
68+
/// <param name="orderBy">An expression to order the results.</param>
69+
/// <param name="ascending">A boolean indicating whether the order should be ascending.</param>
70+
/// <returns>
71+
/// A task that represents the asynchronous operation. The task result contains a <see cref="PaginatedResult{TEntity}"/>
72+
/// with the paginated entities.
73+
/// </returns>
74+
Task<PaginatedResult<TEntity>> GetAllPagingAsync(int pageNumber, int pageSize,
75+
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includes = null!,
76+
Expression<Func<TEntity, bool>> filter = null!,
77+
Expression<Func<TEntity, object>> orderBy = null!, bool ascending = true);
6078
}

src/ClassLibrary.EFCore/Repository.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,49 @@ public async Task<PaginatedResult<TEntity>> GetPaginatedAsync(IQueryable<TEntity
133133

134134
return result;
135135
}
136+
137+
/// <summary>
138+
/// Retrieves a paginated list of entities asynchronously with optional filtering, ordering, and including related entities.
139+
/// </summary>
140+
/// <param name="pageNumber">The page number to retrieve.</param>
141+
/// <param name="pageSize">The number of items per page.</param>
142+
/// <param name="includes">A function to include related entities in the query.</param>
143+
/// <param name="filter">A filter expression to apply to the entities.</param>
144+
/// <param name="orderBy">An expression to order the results.</param>
145+
/// <param name="ascending">A boolean indicating whether the order should be ascending.</param>
146+
/// <returns>
147+
/// A task that represents the asynchronous operation. The task result contains a <see cref="PaginatedResult{TEntity}"/> with the paginated entities.
148+
/// </returns>
149+
public async Task<PaginatedResult<TEntity>> GetAllPagingAsync(int pageNumber, int pageSize,
150+
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includes = null!, Expression<Func<TEntity, bool>> filter = null!,
151+
Expression<Func<TEntity, object>> orderBy = null!, bool ascending = true)
152+
{
153+
var query = DbContext.Set<TEntity>().AsNoTracking();
154+
155+
if (includes != null)
156+
{
157+
query = includes(query);
158+
}
159+
160+
if (filter != null)
161+
{
162+
query = query.Where(filter);
163+
}
164+
165+
if (orderBy != null)
166+
{
167+
query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);
168+
}
169+
170+
var totalItems = await query.CountAsync();
171+
var items = await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync();
172+
173+
return new PaginatedResult<TEntity>
174+
{
175+
CurrentPage = pageNumber,
176+
PageSize = pageSize,
177+
TotalItems = totalItems,
178+
Items = items
179+
};
180+
}
136181
}

0 commit comments

Comments
 (0)