Skip to content

Utilities and extensions for Collections includes Collections.Paginable and so on...

License

Notifications You must be signed in to change notification settings

dotnetcore/Collections

Repository files navigation

Collections

Member project of .NET Core Community GitHub license FOSSA Status

NCC Collections consists of a set of collection-based extensions and tools, such as paging extensions.

Nuget Packages

Package Name Version Downloads
DotNetCore.Collections.Paginable
DotNetCore.Collections.Paginable.Chloe
DotNetCore.Collections.Paginable.DosOrm
DotNetCore.Collections.Paginable.EntityFrameworkCore
DotNetCore.Collections.Paginable.FreeSql
DotNetCore.Collections.Paginable.NHibernate
DotNetCore.Collections.Paginable.SqlKata
DotNetCore.Collections.Paginable.SqlSugar

Usage

Install the package

Install-Package DotNetCore.Collections.Paginable

Write code

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;
}

Work with IQueryable<T>

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.

Work with ORMs

For Chloe ORM

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;
    }
}

For Dos.ORM

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"), };
}

For FreeSql

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);
    }
}

For SqlSugar

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;
//...

For NHibernate

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;
    //...
}

For Microsoft.EntityFrameworkCore

//... 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;
    //...
}
//...

For SqlKata with Dapper

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;
    //...
}

Examples

License

Member project of The NCC, MIT

FOSSA Status

About

Utilities and extensions for Collections includes Collections.Paginable and so on...

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •