Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 4235589

Browse files
committed
Aggiunto nuovo metodo EFCore
1 parent 609cd18 commit 4235589

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/NET6CustomLibrary/EFCore/Infrastructure/Interfaces/IDatabaseRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public interface IDatabaseRepository<TEntity, TKey> : IDatabase<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
44
{
5+
Task<ListViewModel<TEntity>> GetEntitiesPaginationAsync(int pageIndex, int pageSize);
56
Task<List<TEntity>> GetOrderByIdAscendingAsync();
67
Task<List<TEntity>> GetOrderByIdDescendingAsync();
78
Task<int> GetCountAsync();

src/NET6CustomLibrary/EFCore/Infrastructure/Repository/DatabaseRepository.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ public DatabaseRepository(DbContext dbContext) : base(dbContext)
88

99
public async Task<int> GetCountAsync()
1010
{
11-
return await DbContext.Set<TEntity>()
12-
.CountAsync();
11+
//return await DbContext.Set<TEntity>()
12+
// .CountAsync();
13+
14+
var result = await DbContext.Set<TEntity>()
15+
.AsNoTracking()
16+
.ToListAsync();
17+
18+
var itemCount = result.Count;
19+
20+
return itemCount;
1321
}
1422

1523
public async Task<List<TEntity>> GetOrderByIdAscendingAsync()
@@ -27,4 +35,27 @@ public async Task<List<TEntity>> GetOrderByIdDescendingAsync()
2735
.AsNoTracking()
2836
.ToListAsync();
2937
}
38+
39+
public async Task<ListViewModel<TEntity>> GetEntitiesPaginationAsync(int pageIndex, int pageSize)
40+
{
41+
//return await DbContext.Set<TEntity>()
42+
// .Skip((pageIndex - 1) * pageSize)
43+
// .Take(pageSize)
44+
// .AsNoTracking()
45+
// .ToListAsync();
46+
47+
var result = await DbContext.Set<TEntity>()
48+
.Skip((pageIndex - 1) * pageSize)
49+
.Take(pageSize)
50+
.AsNoTracking()
51+
.ToListAsync();
52+
53+
//var itemCount = result.Count;
54+
55+
return new ListViewModel<TEntity>
56+
{
57+
Results = result,
58+
TotalCount = result.Count
59+
};
60+
}
3061
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NET6CustomLibrary.EFCore;
2+
3+
public class ListViewModel<T>
4+
{
5+
public List<T> Results { get; set; }
6+
public int TotalCount { get; set; }
7+
}

0 commit comments

Comments
 (0)