DotnetStandardQueryBuilder.Core 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotnetStandardQueryBuilder.Core --version 1.0.3                
NuGet\Install-Package DotnetStandardQueryBuilder.Core -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotnetStandardQueryBuilder.Core" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotnetStandardQueryBuilder.Core --version 1.0.3                
#r "nuget: DotnetStandardQueryBuilder.Core, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DotnetStandardQueryBuilder.Core as a Cake Addin
#addin nuget:?package=DotnetStandardQueryBuilder.Core&version=1.0.3

// Install DotnetStandardQueryBuilder.Core as a Cake Tool
#tool nuget:?package=DotnetStandardQueryBuilder.Core&version=1.0.3                

DotnetStandardQueryBuilder

A Odata compliant Query Builder built using Dotnet Standard 2.0 for MongoDB, SQL, Azure Cosmos Db database

Features

  1. Using DotnetStandardQueryBuilder.Odata packages provides Odata compliant any Query String to Middleware Request object to further build query using available database query builders
  2. Support for DotnetStandardQueryBuilder.Mongo for Mongo Database
  3. Support for DotnetStandardQueryBuilder.Sql for Sql Database
  4. Support for DotnetStandardQueryBuilder.AzureCosmosSql for Azure Cosmos database for Sql Query

How to use

  1. Parsing Odata Query to Request Object

    Install-Package DotnetStandardQueryBuilder.Core
    Install-Package DotnetStandardQueryBuilder.Odata
    
    var request = new UriParser().Parse<T>('?$select=id&$filter=(id eq 545648 and name='DotnetStandardQueryBuilder')&top=10');
    

    Let's see how you can use it in web api

    using DotnetStandardQueryBuilder.OData;
    [ApiController]
    [Route("[controller]")]
    public abstract class TestController<T> : ControllerBase
    {
        private readonly IService<T> _service;
    
        public TestController(IService<T> service)
        {
            _service = service;
        }
    
        [HttpGet]
        public async Task<dynamic> GetAsync()
        {
            var request = new UriParser().Parse<T>(Request.QueryString.ToString());
    
            if (request.Count)
            {
                return await _service.PaginateAsync(request);
            }
    
            return await _service.GetAsync(request);
        }
    }
    
  2. Using Request Object to build queries for Mongo,

    var query = new MongoQueryBuilder<T>(request, _mongoCollection).Query();
    

    or you can simply use extension method

    var query = _mongoCollection.Query(request);
    

    Example,

    using MongoDB.Driver;
    using DotnetStandardQueryBuilder.Core;
    using System.Threading.Tasks;
    
    public class BaseRepository<T>
        where T : BaseModel
    {
        private readonly IMongoCollection<T> _mongoCollection;
    
        public BaseRepository(IDbOptions dbOptions, string collectionName)
        {
            var client = new MongoClient(dbOptions.ConnectionString);
            var database = client.GetDatabase(dbOptions.DatabaseName);
    
            _mongoCollection = database.GetCollection<T>(collectionName);
        }
    
        public async Task<List<T>> GetAsync(IRequest request = null)
        {
            var query = new MongoQueryBuilder<T>(request, _mongoCollection).Query();
    
            return await Task.FromResult(query.ToList());
        }
    }
    
  3. Using Request Object to build queries for Sql,

    var tableName = "Users";
    var sqlQueryBuilder = new SqlQueryBuilder(request, tableName);
    var sqlQuery = sqlQueryBuilder.Query();
    var sqlCountQuery = sqlQueryBuilder.QueryCount();
    

    or you can simply use SqlExpression class and extension methods

    var tableName = "Users";
    var sqlExpression = new SqlExpression(request).Where().Select(tableName).OrderBy().Paginate();
    
    var sqlExpression = new SqlExpression(request).Where().Select(tableName).OrderBy();
    

    The output Sql Query class provides output as expression and both values in seperate properties so you can pass them directly to avoid SqlInjection

    public class SqlQuery
    {
        public string Query { get; set; }
    
        public Dictionary<string, object> Values { get; set; }
    }
    
    1. In the same way, you can build queries for Azure Cosmos Data for Sql Query,
    var azureCosmosSqlQueryBuilder = new AzureCosmosSqlQueryBuilder(request);
    var azureCosmosSqlQuery = azureCosmosSqlQueryBuilder.Query();
    var azureCosmosCountSqlQuery = azureCosmosSqlQueryBuilder.QueryCount();
    
  4. You can directly use Request object to even build queries from service to service without dependant on Odata query string

    var request = new Request
    {
        Filter = new CompositeFilter
        {
            LogicalOperator = LogicalOperator.And,
            Filters = new List<IFilter>
            {
                new Filter
                {
                    Property = "id",
                    Operator = FilterOperator.IsEqualTo,
                    Value = 457785
                },
                new CompositeFilter
                {
                    LogicalOperator = LogicalOperator.Or,
                    Filters = new List<IFilter>
                    {
                        new Filter
                        {
                            Property = "firstName",
                            Operator = FilterOperator.Contains,
                            Value = "DotnetStandard"
                        },
                        new Filter
                        {
                            Property = "lastName",
                            Operator = FilterOperator.StartsWith,
                            Value = "QueryBuilder"
                        },
                    }
                },
                age > 10 
                ?   new Filter
                    {
                        Property = "name",
                        Operator = FilterOperator.IsNotEqualTo,
                        Value = "DotnetStandardQueryBuilder"
                    } 
                : null
            }
        },
        Page = 2,
        PageSize = 10,
        Select = new List<string> { "id", "name" },
        Count = true
    };
    

    Request Class

    public class Request
    {
        public int Page { get; set; } = 1;
    
        public int? PageSize { get; set; }
    
        public bool Count { get; set; }
    
        public IFilter Filter { get; set; }
    
        public List<Sort> Sorts { get; set; }
    
        public List<string> Select { get; set; }
    
        public bool Distinct { get; set; }
    }
    

Feel free contribute and raise PR's

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on DotnetStandardQueryBuilder.Core:

Package Downloads
DotnetStandardQueryBuilder.Sql

DotnetStandardQueryBuilder.Mongo package contains methods to build SQL database query from query builder request

DotnetStandardQueryBuilder.OData

DotnetStandardQueryBuilder.OData package contains methods to build query request from http request query string

DotnetStandardQueryBuilder.Mongo

DotnetStandardQueryBuilder.Mongo package contains methods to build Mongo database query from query builder request

DotnetStandardQueryBuilder.AzureCosmosSql

DotnetStandardQueryBuilder.AzureCosmosSql package contains methods to build Azure Cosmos database SQL query from query builder request

DotnetStandardQueryBuilder.MemoryList

DotnetStandardQueryBuilder.MemoryList package contains methods to filter in memory list objects by using query builder request

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 4,247 1/6/2023
1.1.2 12,142 4/8/2021
1.1.1 1,566 4/2/2021
1.1.0 1,512 3/18/2021
1.0.9 1,412 3/18/2021
1.0.8 1,396 3/15/2021
1.0.7 1,364 3/14/2021
1.0.6 1,531 3/14/2021
1.0.5 1,358 3/13/2021
1.0.4 1,379 3/13/2021
1.0.3 1,414 3/13/2021
1.0.2 1,170 3/12/2021
1.0.1 1,034 3/12/2021
1.0.0 1,074 3/12/2021

A OData compliant QueyBuilder for net and aspnet core application for different MongoDB, SQL databases