An open API service indexing awesome lists of open source software.

https://github.com/mgernand/litedb.queryable

A queryable wrapper implementation for LiteDB with additional async extensions.
https://github.com/mgernand/litedb.queryable

async dotnet dotnet-core dotnet7 linq litedb netstandard netstandard20 netstandard21 queryable

Last synced: 2 months ago
JSON representation

A queryable wrapper implementation for LiteDB with additional async extensions.

Awesome Lists containing this project

README

          

# LiteDB.Queryable

An IQueryable wrapper implementation for LiteDB with additional async extensions.

This library allows the use of LINQ extensions methods for querying LiteDB.

The ```LiteQueryable``` implementation is a warpper around a ```ILiteCollection```
or a ```ILiteCollectionAsync<>```. The LINQ extenions call are delegated to the
equivalent methods of the LiteDB API.

## This repository was moved to https://codeberg.org/mgernand/LiteDB.Queryable

## Usage

Add the NuGet package to your project: ```LiteDB.Queryable```

You can then aquire an ```IQueryable``` instance using one of the available ```AsQueryable```
extension methods for ```ILiteCollection``` or ```ILiteCollectionAsync```. The async
variant is provided by the [litedb-async](https://github.com/mlockett42/litedb-async) project.

```C#
LiteDatabase database = new LiteDatabase("test.db");
ILiteCollection collection = database.GetCollection("people");
IQueryable queryable = collection.AsQueryable();
```

or

```C#
LiteDatabaseAsync database = new LiteDatabaseAsync("test.db");
ILiteCollectionAsync collection = database.GetCollection("people");
IQueryable queryable = collection.AsQueryable();
```

After that you can use the synchronous LINQ extensions with both variants and the asynchronous
ones with the second variant.

## Supported LINQ extensions

You can use the following methods to configure your query. Those methods are available in both
variants.

- Where
- OrderBy
- OrderByDescending
- Skip
- Take
- Include
- Select

The ```ThenBy/ThenByDescending``` methods and multiple OrderBy/OrderByDescending class are not
supported at the moment, because LiteDB doesn't support multiple order exprssions.

### Synchronous extenions

- ToList
- ToArray
- ToDictionary
- First
- FirstOrDefault
- Single
- SingleOrDefault
- Count
- LongCount
- Any
- Sum
- Average
- Min
- Max

### Asynchronous extenions

- ToListAsync
- ToArrayAsync
- ToDictionaryAsync
- FirstAsync
- FirstOrDefaultAsync
- SingleAsync
- SingleOrDefaultAsync
- CountAsync
- LongCountAsync
- AnyAsync
- SumAsync
- AverageAsync
- MinAsync
- MaxAsync

## Example

```C#
LiteDatabase database = new LiteDatabase("test.db");
ILiteCollection collection = database.GetCollection("people");
IQueryable queryable = collection.AsQueryable();

IList result = queryable
.Where(x => x.Name.StartsWith("T"))
.OrderBy(x => x.Age)
.ToList();

Person result = queryable
.Where(x => x.Age > 35)
.FirstOrDefault();

string result = queryable
.Where(x => x.Age > 35)
.Select(x => x.Name)
.FirstOrDefault();
```

or

```C#
LiteDatabaseAsync database = new LiteDatabaseAsync("test.db");
ILiteCollectionAsync collection = database.GetCollection("people");
IQueryable queryable = collection.AsQueryable();

IList result = await queryable
.Where(x => x.Name.StartsWith("T"))
.OrderBy(x => x.Age)
.ToListAsync();

Person result = await queryable
.Where(x => x.Age > 35)
.FirstOrDefaultAsync();

string result = await queryable
.Where(x => x.Age > 35)
.Select(x => x.Name)
.FirstOrDefaultAsync();
```

## References

- [LiteDB](https://github.com/mbdavid/LiteDB)
- [LiteDB Async](https://github.com/mlockett42/litedb-async)
- [Entity Framework Core](https://github.com/dotnet/efcore)