QueryBuilder.Net 0.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package QueryBuilder.Net --version 0.3.0
                    
NuGet\Install-Package QueryBuilder.Net -Version 0.3.0
                    
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="QueryBuilder.Net" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryBuilder.Net" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="QueryBuilder.Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add QueryBuilder.Net --version 0.3.0
                    
#r "nuget: QueryBuilder.Net, 0.3.0"
                    
#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.
#addin nuget:?package=QueryBuilder.Net&version=0.3.0
                    
Install QueryBuilder.Net as a Cake Addin
#tool nuget:?package=QueryBuilder.Net&version=0.3.0
                    
Install QueryBuilder.Net as a Cake Tool

QueryBuilder.NET

This is a prerelase unfinished version

QueryBuilder.NET is a simple SQL query generator based on Dapper and Dapper.ColumnMapper that helps building sql queries in a fluent builder-like style.

In this early version:

  • only Insert and Delete command is implemented
  • only PostgreSQL syntax is supported
  • only primitive straigh-forward WHERE clauses are supported

How it works

Let's say you have some model class, for eg. Person:

public class Person
{
    public Guid Id { get; set; }
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";    
    public DateTime DateOfBirth { get; set; }
    public string Email { get; set; } = "";
    public int Age { get; set; }
    public decimal Salary { get; set; }
}

If you want to create CRUD operations for it using Dapper, you start writing SQL query like this:

string insertQuery = $@"
                    INSERT INTO ""Persons""
                    (
                        ""FirstName"",
                        ""LastName"",
                        ""DateOfBirth"",
                        ""Email"",
                        ""Age"",
                        ""Salary""
                    )
                    VALUES
                    (
                        @firstName,
                        @lastName,
                        @dateOfBirth,
                        @email,
                        @age,
                        @salary
                    )
                    RETURNING ""Id""; ";

It looks pretty easy, but as your model classes grow and become more complicated, you have to edit these queries and keep them up to date. In larger projects this is a rutine job, that is very error prone and boring. You don't want to focus on commas and quotes in SQL queries, when you have deadline upon you.

And with QueryBuilder.NET you can write just this:

//person is instance of model you want to save
var dapperQuery = SqlQueryBuilder.InsertInto(person, "Persons").ReturningId().BuildQuery();

It will generate sql command exactly like the one above and also dynamic parameters filled from the instance you provided. Then to execute it:

var insertedId = await connection.ExecuteScalarAsync<Guid>(dapperQuery.CommandText, dapperQuery.Parameters);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.1 2,931 2/27/2025
0.3.0 791 2/25/2025
0.2.3 102 2/24/2025