/// <summary> /// Gets a collection of entities from the <typeparamref name="TEntity"/> table which match the <paramref name="conditions"/>. /// </summary> /// <example> /// <code> /// <![CDATA[ /// [Table("Users")] /// public class UserEntity /// { /// [Key] /// public int Id { get; set; } /// /// public string Name { get; set; } /// /// public int Age { get; set; } /// } /// ... /// var pageBuilder = new PageIndexPageBuilder(3, 10); /// var users = await this.connection.GetPageAsync<UserEntity>(pageBuilder, new { Age = 10 }, "Age DESC"); /// ]]> /// </code> /// </example> public static async Task <PagedList <TEntity> > GetPageAsync <TEntity>( this IDbConnection connection, IPageBuilder pageBuilder, object conditions, string orderBy, IDbTransaction transaction = null, IDialect dialect = null, int?commandTimeout = null, CancellationToken cancellationToken = default(CancellationToken)) { Ensure.NotNull(connection, nameof(connection)); var totalNumberOfItems = await connection.CountAsync <TEntity>(conditions, transaction, dialect, commandTimeout, cancellationToken) .ConfigureAwait(false); var page = pageBuilder.GetCurrentPage(totalNumberOfItems); if (page.IsEmpty) { return(PagedList <TEntity> .Empty(totalNumberOfItems, page)); } var itemsCommand = CommandFactory.MakeGetPageCommand <TEntity>(page, conditions, orderBy, transaction, dialect, commandTimeout, cancellationToken); var items = await connection.QueryAsync <TEntity>(itemsCommand).ConfigureAwait(false); return(PagedList <TEntity> .Create(totalNumberOfItems, page, items)); }
/// <summary> /// Gets a collection of entities from the <typeparamref name="TEntity"/> table which match the <paramref name="conditions"/>. /// </summary> /// <example> /// <code> /// <![CDATA[ /// [Table("Users")] /// public class UserEntity /// { /// [Key] /// public int Id { get; set; } /// /// public string Name { get; set; } /// /// public int Age { get; set; } /// } /// ... /// var pageBuilder = new PageIndexPageBuilder(3, 10); /// var users = this.connection.GetPage<UserEntity>(pageBuilder, "WHERE Age > @MinAge", "Age DESC", new { MinAge = 18 }); /// ]]> /// </code> /// </example> public static PagedList <TEntity> GetPage <TEntity>( this IDbConnection connection, IPageBuilder pageBuilder, string conditions, string orderBy, object parameters = null, IDbTransaction transaction = null, IDialect dialect = null, int?commandTimeout = null) { Ensure.NotNull(connection, nameof(connection)); var totalNumberOfItems = connection.Count <TEntity>(conditions, parameters, transaction, dialect, commandTimeout); var page = pageBuilder.GetCurrentPage(totalNumberOfItems); if (page.IsEmpty) { return(PagedList <TEntity> .Empty(totalNumberOfItems, page)); } var itemsCommand = CommandFactory.MakeGetPageCommand <TEntity>(page, conditions, orderBy, parameters, transaction, dialect, commandTimeout); var items = connection.Query <TEntity>(itemsCommand); return(PagedList <TEntity> .Create(totalNumberOfItems, page, items)); }
public PagedList <TEntity> GetPage <TEntity>(IPageBuilder pageBuilder, string orderBy, object conditions, int?commandTimeout = null) { var totalNumberOfItems = this.Count <TEntity>(conditions, commandTimeout); var page = pageBuilder.GetCurrentPage(totalNumberOfItems); if (page.IsEmpty) { return(PagedList <TEntity> .Empty(totalNumberOfItems, page)); } var pageCommand = this.commandFactory.MakeGetPageCommand <TEntity>(page, conditions, orderBy); var items = this.Query <TEntity>(pageCommand.CommandText, pageCommand.Parameters, CommandType.Text, commandTimeout); return(PagedList <TEntity> .Create(totalNumberOfItems, page, items)); }
public async Task <PagedList <TEntity> > GetPageAsync <TEntity>(IPageBuilder pageBuilder, string orderBy, object conditions, int?commandTimeout = null, CancellationToken cancellationToken = default) { var totalNumberOfItems = await this.CountAsync <TEntity>(conditions, commandTimeout, cancellationToken).ConfigureAwait(false); var page = pageBuilder.GetCurrentPage(totalNumberOfItems); if (page.IsEmpty) { return(PagedList <TEntity> .Empty(totalNumberOfItems, page)); } var itemsCommand = this.commandFactory.MakeGetPageCommand <TEntity>(page, conditions, orderBy); var items = await this.QueryAsync <TEntity>(itemsCommand.CommandText, itemsCommand.Parameters, CommandType.Text, commandTimeout, cancellationToken).ConfigureAwait(false); return(PagedList <TEntity> .Create(totalNumberOfItems, page, items)); }