Dapper-QueryBuilder
1.1.1
See the version list below for details.
dotnet add package Dapper-QueryBuilder --version 1.1.1
NuGet\Install-Package Dapper-QueryBuilder -Version 1.1.1
<PackageReference Include="Dapper-QueryBuilder" Version="1.1.1" />
paket add Dapper-QueryBuilder --version 1.1.1
#r "nuget: Dapper-QueryBuilder, 1.1.1"
// Install Dapper-QueryBuilder as a Cake Addin #addin nuget:?package=Dapper-QueryBuilder&version=1.1.1 // Install Dapper-QueryBuilder as a Cake Tool #tool nuget:?package=Dapper-QueryBuilder&version=1.1.1
Dapper Query Builder
Dapper Query Builder using String Interpolation and Fluent API
This library is a wrapper around Dapper mostly for helping building dynamic SQL queries and commands.
String Interpolation instead of manually using DynamicParameters
By using interpolated strings we can pass parameters to Dapper without having to worry about creating and managing DynamicParameters manually.
You can build your queries with interpolated strings, and this library will automatically "parametrize" your values.
var products = cn
.QueryBuilder($@"
SELECT * FROM [Product]
WHERE
[Name] LIKE {productName}
AND [ProductSubcategoryID] = {subCategoryId}
ORDER BY [ProductId]").Query<Product>;
The underlying query will be fully parametrized ([Name] LIKE @p0 AND [ProductSubcategoryID] = @p1
), without risk of SQL-injection, even though it looks like you're just building dynamic sql.
Query and Parameters walk side-by-side
QueryBuilder basically wraps 2 things that should always stay together: the query which you're building, and the parameters which must go together with your query.
This is a simple concept but it allows us to add new sql clauses (parametrized) in a single statement.
Let's say you're building a query with a variable number of conditions:
var query = cn.QueryBuilder($"SELECT * FROM [Product] WHERE 1=1");
query.Append($" AND Name LIKE {productName}");
query.Append($" AND ProductSubcategoryID = {subCategoryId}");
var products = query.Query<Product>();
QueryBuilder will wrap both the Query and the Parameters, so that you can easily append new sql statements (and parameters) easily.
When you invoke Query, the underlying query and parameters are passed to Dapper.
Filters as First-class citizen
As shown above, you'll still write plain SQL, which is what we all love about Dapper.
Since the most common use case for dynamic clauses is adding WHERE parameters, the library offers WHERE filters as a special structure:
- You can add filters to QueryBuilder using .Where() method, those filters are saved internally
- When you send your query to Dapper, QueryBuilder will search for a
/**where**/
statement in your query and will replace with the filters you defined.
So you can still write your queries on your own, and yet benefit from string interpolation (which is our mojo and charm) and from dynamically building a list of filters.
int maxPrice = 1000;
int maxWeight = 15;
string search = "%Mountain%";
var cn = new SqlConnection(connectionString);
// You can build the query manually and just use QueryBuilder to replace "where" filters (if any)
var q = cn.QueryBuilder(@"SELECT ProductId, Name, ListPrice, Weight
FROM [Product]
/**where**/
ORDER BY ProductId
");
// You just pass the parameters as if it was an interpolated string,
// and QueryBuilder will automatically convert them to Dapper parameters (injection-safe)
q.Where($"[ListPrice] <= {maxPrice}");
q.Where($"[Weight] <= {maxWeight}");
q.Where($"[Name] LIKE {search}");
// Query() will automatically build your query and replace your /**where**/ (if any filter was added)
var products = q.Query<Product>();
Combining Filters
var q = cn.QueryBuilder(@"SELECT ProductId, Name, ListPrice, Weight
FROM [Product]
/**where**/
ORDER BY ProductId
");
q.Where(new Filters()
{
new Filter($"[ListPrice] >= {minPrice}"),
new Filter($"[ListPrice] <= {maxPrice}")
});
q.Where(new Filters(Filters.FiltersType.OR)
{
new Filter($"[Weight] <= {maxWeight}"),
new Filter($"[Name] LIKE {search}")
});
var products = q.Query<Product>();
// Query() will automatically build your SQL query, and will replace your /**where**/ (if any filter was added)
// "WHERE ([ListPrice] >= @p0 AND [ListPrice] <= @p1) AND ([Weight] <= @p2 OR [Name] LIKE @p3)"
// it will also pass an underlying DynamicParameters object, with all parameters you passed using string interpolation
// (@p0 as minPrice, @p1 as maxPrice, etc..)
Raw command building
If you don't like the "magic" of replacing /**where**/
filters, you can do everything on your own.
// start your basic query
var q = cn.QueryBuilder(@"SELECT ProductId, Name, ListPrice, Weight FROM [Product] WHERE 1=1");
// append whatever statements you need (.Append instead of .Where!)
q.Append($"AND [ListPrice] <= {maxPrice}");
q.Append($"AND [Weight] <= {maxWeight}");
q.Append($"AND [Name] LIKE {search}");
q.Append($"ORDER BY ProductId");
var products = q.Query<Product>();
IN lists
Dapper allows us to use IN lists magically. And it also works with our string interpolation:
var q = cn.QueryBuilder($@"
SELECT c.[Name] as [Category], sc.[Name] as [Subcategory], p.[Name], p.[ProductNumber]
FROM [Product] p
INNER JOIN [ProductSubcategory] sc ON p.[ProductSubcategoryID]=sc.[ProductSubcategoryID]
INNER JOIN [ProductCategory] c ON sc.[ProductCategoryID]=c.[ProductCategoryID]");
var categories = new string[] { "Components", "Clothing", "Acessories" };
q.Append($"WHERE c.[Name] IN {categories}");
Fluent API (Chained-methods)
var q = cn.QueryBuilder()
.Select($"ProductId")
.Select($"Name")
.Select($"ListPrice")
.Select($"Weight")
.From($"[Product]")
.Where($"[ListPrice] <= {maxPrice}")
.Where($"[Weight] <= {maxWeight}")
.Where($"[Name] LIKE {search}")
.OrderBy($"ProductId");
var products = q.Query<Product>();
Building joins dynamically using Fluent API:
var categories = new string[] { "Components", "Clothing", "Acessories" };
var q = cn.QueryBuilder()
.SelectDistinct($"c.[Name] as [Category], sc.[Name] as [Subcategory], p.[Name], p.[ProductNumber]")
.From($"[Product] p")
.From($"INNER JOIN [ProductSubcategory] sc ON p.[ProductSubcategoryID]=sc.[ProductSubcategoryID]")
.From($"INNER JOIN [ProductCategory] c ON sc.[ProductCategoryID]=c.[ProductCategoryID]")
.Where($"c.[Name] IN {categories}");
There are also chained-methods for adding GROUP BY, HAVING, ORDER BY, and paging (OFFSET x ROWS / FETCH NEXT x ROWS ONLY).
nameof() and raw strings
For those who like strongly typed queries, you can also use nameof
expression, but you have to define format "raw" such that the string is preserved and it's not converted into a @parameter.
var q = cn.QueryBuilder($@"
SELECT
c.[{nameof(Category.Name):raw}] as [Category],
sc.[{nameof(Subcategory.Name):raw}] as [Subcategory],
p.[{nameof(Product.Name):raw}],
p.[{nameof(Product.ProductNumber):raw}]"
FROM [Product] p
INNER JOIN [ProductSubcategory] sc ON p.[ProductSubcategoryID]=sc.[ProductSubcategoryID]
INNER JOIN [ProductCategory] c ON sc.[ProductCategoryID]=c.[ProductCategoryID]");
And in case you can use "find references", "rename" (refactor), etc.
See full documentation here
Product | Versions 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 is compatible. 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. |
-
.NETFramework 4.7.2
- Dapper (>= 2.0.35)
- Microsoft.CSharp (>= 4.7.0)
-
.NETStandard 2.0
- Dapper (>= 2.0.35)
- Microsoft.CSharp (>= 4.7.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Dapper-QueryBuilder:
Package | Downloads |
---|---|
DapperChunk
感谢开源库 Dapper DapperQueryBuilder 1.Dapper Chunks 用于大数据处理 2.Dapper Sqlbuilder sql 构建器,仿照 开源库 FluentQueryBuilder 构建.(目前实现 mysql,mssql,sqlite), FluentQueryExtensions.Register注册 数据库类型与构建器的映射关系 |
|
ViraVenda.Core
Biblioteca core do aplicativo Vira Venda |
|
Codibre.GrpcSqlProxy.Client
Package Description |
|
Harbin.DataAccess
Data Access library based on Dapper, Dapper.FastCRUD, and DapperQueryBuilder. Implement Repositories (Generic Repository Pattern) and helps to manage connections to distributed databases and/or read-replicas. |
|
Harbin.DataAccess.DapperFastCRUD
Data Access library based on Dapper, Dapper.FastCRUD, and DapperQueryBuilder. Implement Repositories (Generic Repository Pattern) and helps to manage connections to distributed databases and/or read-replicas. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
2.0.0 | 32,113 | 3/17/2024 | |
2.0.0-beta2 | 106 | 3/14/2024 | |
2.0.0-beta1 | 1,498 | 7/10/2023 | |
1.2.9 | 274,240 | 1/7/2023 | |
1.2.8 | 410 | 1/7/2023 | |
1.2.7 | 114,819 | 7/31/2022 | |
1.2.6 | 1,052 | 7/28/2022 | |
1.2.5 | 73,119 | 5/9/2022 | |
1.2.4 | 15,867 | 3/6/2022 | |
1.2.3 | 4,013 | 2/22/2022 | |
1.2.2 | 4,268 | 2/13/2022 | |
1.2.1 | 539 | 2/13/2022 | |
1.2.0 | 91,287 | 11/4/2021 | |
1.1.1 | 6,588 | 10/24/2021 | |
1.1.0 | 159,089 | 5/1/2021 | |
1.0.9 | 4,028 | 4/20/2021 | |
1.0.8 | 7,619 | 3/30/2021 | |
1.0.7 | 52,332 | 3/6/2021 | |
1.0.6 | 4,508 | 2/9/2021 | |
1.0.5 | 14,898 | 12/25/2020 | |
1.0.4 | 39,610 | 8/18/2020 | |
1.0.3 | 832 | 8/6/2020 | |
1.0.2 | 623 | 8/5/2020 | |
1.0.1 | 581 | 8/3/2020 | |
1.0.0 | 1,916 | 8/2/2020 |