using LinqToDB.AspNet.Logging;
using LinqToDB.DataProvider.SQLite;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;
public static async Task Main()
Console.WriteLine("Hello World");
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
using var sp = new ServiceCollection()
.AddLogging(x => x.AddConsole())
.AddSingleton(serviceProvider => {
var connection = new SqliteConnection("Data Source=dbname;Mode=Memory;Cache=Shared");
var provider = SQLiteTools.GetDataProvider();
var options = new DataOptions()
.UseConnection(provider, connection)
.UseLoggerFactory(serviceProvider.GetService<ILoggerFactory>())
return new DataConnection(options);
using var connection = sp.GetService<DataConnection>();
await connection.CreateTableAsync<TestTable>();
var rows = new List<TestTable>();
foreach(var i in Enumerable.Range(1, 40))
await connection.BulkCopyAsync(rows);
var query = connection.GetTable<TestTable>()
.Where(x => x.DecimalProp > myDecimal);
var (result, totalCount) = await QueryPaginator.Paginate<TestTable>(query, 0, 0, CancellationToken.None);
Console.WriteLine("final");
Console.WriteLine("No crash? Found " + result.Count() + ", total: "+ totalCount);
public int Id { get; set; }
public decimal DecimalProp { get; set; }
public static class QueryPaginator
private record Envelope<T>(int TotalCount, T Data);
public static async Task<(List<T> Data, int TotalCount)> Paginate<T>(
CancellationToken cancellationToken)
var result = new List<T>(take);
if (cancellationToken.IsCancellationRequested)
throw new TaskCanceledException();
var queryWithCount = query
.Select(data => new Envelope<T>(Sql.Ext.Count().Over().ToValue(), data))
.WithCancellation(cancellationToken);
await foreach (var row in queryWithCount)
totalCount ??= row.TotalCount;
return (result, totalCount ?? 0);