NCC Collections consists of a set of collection-based extensions and tools, such as paging extensions.
Install-Package DotNetCore.Collections.Paginable
IEnumerable<ExampleModel> list = GetList();//...
//Get a collection of Page, each page has 50 PageMembers
var paginableList = list.ToPaginable(50);
//Get page 15th
var page = paginableList.GetPage(15);
for (var i = 0; i < page.CurrentPageSize; i++)
{
var itemNumber = page[i].ItemNumber;
var itemValue = page[i].Value;
}
Or use a more streamlined code:
IEnumerable<ExampleModel> list = GetList();//...
//Get page 15th, each page has 50 items.
ar page = list.GetPage(15, 50);
for (var i = 0; i < page.CurrentPageSize; i++)
{
var itemNumber = page[i].ItemNumber;
var itemValue = page[i].Value;
}
You can get IQueryable<T>
from Where
in EfCore or Query<T>
in NHibernate, and then:
IQueryable<ExampleModel> queryable = GetQueryable();//...
var page = queryable.GetPage(15, 50);
var totalMemberCount = page.TotalMemberCount;
for(var i = 0; i < page.CurrentPageSize; i++)
{
var itemNumber = page[i].ItemNumber;
var itemValue = page[i].Value;
}
Just do it.
Install DotNetCore.Collections.Paginable.Chloe
package:
Install-Package DotNetCore.Collections.Paginable.Chloe
then:
//... do some config for Chloe by EntityTypeBuilder<ExampleModel>
using(var db = new MsSqlContext(connectionString))
{
var page = db.Query<ExampleModel>().GetPage(15, 50);
var totalPageCount = page.TotalPageCount;
var totalMemberCount = page.TotalMemberCount;
var pageSize = page.PageSize;
var currentPageNumber = page.CurrentPageNumber;
var currentPageSize = page.CurrentPageSize;
var hasNext = page.HasNext;
var HasPrevious = page.HasPrevious;
for(var i = 0; i < currentPageSize; i++)
{
var id = page[i].Value.Id;
}
}
Install DotNetCore.Collections.Paginable.DosOrm
package:
Install-Package DotNetCore.Collections.Paginable.DosOrm
then:
var _session = new DbSession(DatabaseType.SqlServer, connectionString);
var page = _dosOrmSession.From<ExampleModel>().GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
.
.
.
class ExampleModel : Entity
{
public ExampleModel() : base("ExampleModels") { }
public virtual int Id { get; set; }
public override Field[] GetPrimaryKeyFields() => new Field[] { new Field("Id"), };
}
Install DotNetCore.Collections.Paginable.FreeSql
package:
Install-Package DotNetCore.Collections.Paginable.FreeSql
then:
var _freeSql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(DataType.SqlServer, connectionString)
.UseAutoSyncStructure(false)
.Build();
//... do some config for FreeSql
var page = _freeSql.Select<ExampleModel>().GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
or call the extension method of DbSet directly:
var ctx = _freeSql.CreateDbContext();
var source = ctx.Set<ExampleModel>();
var page = source.GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
or
using(var ctx = new ExampleDbContext())
{
var page = ctx.ExampleModels.GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
}
.
.
.
class ExampleDbContext: DbContext
{
public DbSet<ExampleModel> ExampleModel {get; set;}
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseFreeSql(_freeSqlInstance);
}
}
Install DotNetCore.Collections.Paginable.SqlSugar
package:
Install-Package DotNetCore.Collections.Paginable.SqlSugar
then:
var sqlSugar = new SqlSugatClient(new ConnectionConfig{
ConnectionString = connectionString,
DbType = DbTypee.SqlServer,
IsAutoCloseConnection = true
});
//... do some config for sqlSugar
var page = _sqlSugar.Query<ExampleModel>().GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
Install DotNetCore.Collections.Paginable.NHibernate
package:
Install-Package DotNetCore.Collections.Paginable.NHibernate
then:
//... do some config for NHibernate by FluentNHibernate.ClassMap<ExampleModel>
using(var session = GetAndOpenSession())
{
var page = session.QueryOver<ExampleModel>().GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
}
//... do come config for EFCore
using(var context = new ExampleDbContext())
{
var page = context.ExampleModels.Where(x => x.Id > 100).GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
}
or call the extension method of DbSet directly:
Install DotNetCore.Collections.Paginable.EntityFrameworkCore
package first:
Install-Package DotNetCore.Collections.Paginable.EntityFrameworkCore
then:
using(var context = new ExampleDbContext())
{
var pagee = context.ExampleModels.GetPage(1, 9);
var totalPageCount = page.TotalPageCount;
//...
}
//...
Install DotNetCore.Collections.Paginable.SqlKata
package:
Install-Package DotNetCore.Collections.Paginable.SqlKata
then:
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
var compiler = new SqlServerCompiler();
var db = new QueryFactory(connection, compiler);
var page = db.Query("ExampleModels").GetPage<ExampleModel>(1, 9);
var totalPageCount = page.TotalCount;
//...
}
Member project of The NCC, MIT