DotNetBrightener.LinQToSqlBuilder 2025.0.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotNetBrightener.LinQToSqlBuilder --version 2025.0.9
                    
NuGet\Install-Package DotNetBrightener.LinQToSqlBuilder -Version 2025.0.9
                    
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="DotNetBrightener.LinQToSqlBuilder" Version="2025.0.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetBrightener.LinQToSqlBuilder" Version="2025.0.9" />
                    
Directory.Packages.props
<PackageReference Include="DotNetBrightener.LinQToSqlBuilder" />
                    
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 DotNetBrightener.LinQToSqlBuilder --version 2025.0.9
                    
#r "nuget: DotNetBrightener.LinQToSqlBuilder, 2025.0.9"
                    
#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.
#:package DotNetBrightener.LinQToSqlBuilder@2025.0.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetBrightener.LinQToSqlBuilder&version=2025.0.9
                    
Install as a Cake Addin
#tool nuget:?package=DotNetBrightener.LinQToSqlBuilder&version=2025.0.9
                    
Install as a Cake Tool

LinQ to SQL Builder - small .NET library supports creating SQL queries and commands in a strongly typed fashion.

© 2025 DotNet Brightener

NuGet Version

Inspiration

I am a big fan of ORM, and I have been using Entity Framework since the first day I started my career as a .Net Developer back in 2009.

Some time ago I woked on a project and needed to deal with 2 databases at the same time. For some reasons, I am not supposed to use Entity Framework for the second database which is dynamically configured in a tenant-based setting at runtime. So I have to choose either to come back to ADO.Net and making queries using string concatenation and SqlCommand, or I to come up with something that is friendly with Entity Framework usage, which is using Linq lambda expression to describe the query or command we want to process with the database.

Searching through the internet, I found a few repositories that seem to fit my needs, but I still reluctant because they all seem to miss something. For instance, the open-source library from https://github.com/mladenb/sql-query-builder does most of the operations that we need for basic usages, but the queries and commands are built on top of strings.

Finally, I found the open-source repository at https://github.com/DomanyDusan/lambda-sql-builder and it's very close to what I am looking for. However, the author has not continued supporting the project and its last commit was 7 years prior to the time I started developed this library. So I decided to reference his code and make a modified version of what he had done, and adding support for INSERT, UPDATE, DELETE instead of only SELECT queries as in the original version.

This project is not meant to replace or to cover the entire SQL world, the purpose of this is to provide the most basic and commonly used operations like CRUD (Create / Read / Update / Delete) to the database from the application, which I believe it covers 70% the simple data access operations in most applications.

Continuing the spirit of original library, this library can be used to help you generate the query and parameters that you can use in ADO.Net with SqlCommand or you can use with Dapper to have a simple mapping back to your entities.

Huge thanks and credits to the original author DomanyDusan and his tool. And I hope you guys, the developers, find my modified library helpful for your works/projects. All feedbacks and suggestions are welcome so that I can make this tool better.

Installation

Install using Package Reference

dotnet add [YOUR_PROJECT_NAME] package DotNetBrightener.LinQToSqlBuilder

You can optionally specified version by using --version [version] parameter

Usage

Simple Select

This basic example queries the database for 10 User and order them by their registration date using Dapper:

var query = SqlBuilder.Select<User>()
                      .OrderBy(_ => _.RegistrationDate)
                      .Take(10);
                      
var results = Connection.Query<User>(query.CommandText, query.CommandParameters);

As you can see the CommandText property will return the SQL string itself, while the CommandParameters property refers to a dictionary of SQL parameters.

Select query with Join

The below example performs a query to the User table, and join it with UserGroup table to returns a many to many relationship mapping specified using Dapper mapping API

var query = SqlBuilder.Select<User>()
                    //.Where(user => user.Email == email)
                      .Join<UserUserGroup>((@user, @group) => user.Id == group.UserId)
                      .Join<UserGroup>((group,     g) => group.UserGroupId == g.Id)
                      .Where(group => group.Id == groupId);

var result = new Dictionary<long, User>();
var results = Connection.Query<User, UserGroup, User>(query.CommandText,
                                                        (user, group) =>
                                                        {
                                                            if (!result.ContainsKey(user.Id))
                                                            {
                                                                user.Groups = new List<UserGroup>();
                                                                result.Add(user.Id, user);
                                                            }

                                                            result[user.Id].Groups.Add(group);
                                                            return user;
                                                        },
                                                        query.CommandParameters,
                                                        splitOn: "UserId,UserGroupId")
                        .ToList();
Insert single record

The example below will generate an insert command with one record.

var query = SqlBuilder.Insert<UserGroup>(_ => new UserGroup
            {
                CreatedBy   = "TestSystem",
                CreatedDate = DateTimeOffset.Now,
                Description = "Created from Test System",
                Name        = "TestUserGroup",
                IsDeleted   = false
            });

var results = Connection.Execute(query.CommandText, query.CommandParameters);
Insert multiple records

The example below will generate an insert command with multiple records.

var query = SqlBuilder.InsertMany<UserGroup>(_ => new []
            {
                new UserGroup
                {
                    CreatedBy   = "TestSystem",
                    CreatedDate = DateTimeOffset.Now,
                    Description = "Created from Test System",
                    Name        = "TestUserGroup",
                    IsDeleted   = false
                },

                new UserGroup
                {
                    CreatedBy   = "TestSystem",
                    CreatedDate = DateTimeOffset.Now,
                    Description = "Created from Test System",
                    Name        = "TestUserGroup2",
                    IsDeleted   = false
                },

                new UserGroup
                {
                    CreatedBy   = "TestSystem",
                    CreatedDate = DateTimeOffset.Now,
                    Description = "Created from Test System",
                    Name        = "TestUserGroup3",
                    IsDeleted   = false
                }
            });

var results = Connection.Execute(query.CommandText, query.CommandParameters);
Insert by copying from another table

Sometimes we need to copy a bunch of records from one table to another. For instance, if we have an order that contains few products, and the quantity of the products are being updated before the order gets finalized. So we need to keep the inventory history records of all products that are being updated from time to time. Using Entity Framework, we could have loaded all inventory records of the specified products, then create a copied object and insert them to the inventory history. The more products you have, the slower performance you will suffer because you will have to deal with the data that are in memory versus the data that are being processed by other request(s).

var query = SqlBuilder.InsertFrom<Inventory, InventoryHistory>(inventory => new InventoryHistory()
                                   {
                                       CreatedBy        = "Cloning System",
                                       CreatedDate      = DateTimeOffset.Now,
                                       StockQuantity    = inventory.StockQuantity,
                                       ReservedQuantity = inventory.ReservedQuantity,
                                       IsDeleted        = inventory.IsDeleted,
                                       InventoryId      = inventory.Id,
                                       ProductId        = inventory.ProductId
                                   })
                                  .WhereIsIn(inventory => inventory.ProductId, new long[] { /*... obmited values, describes the list of product ids */ });

Assert.AreEqual("INSERT INTO [dbo].[InventoryHistory] ([CreatedBy], [CreatedDate], [StockQuantity], [ReservedQuantity], [IsDeleted], [InventoryId], [ProductId]) " +
                "SELECT " +
                "@Param1 as [CreatedBy], " +
                "@Param2 as [CreatedDate], " +
                "[dbo].[Inventory].[StockQuantity] as [StockQuantity], " +
                "[dbo].[Inventory].[ReservedQuantity] as [ReservedQuantity], " +
                "[dbo].[Inventory].[IsDeleted] as [IsDeleted], " +
                "[dbo].[Inventory].[Id] as [InventoryId] " +
                "[dbo].[Inventory].[ProductId] as [ProductId] " +
                "FROM [dbo].[Inventory] " +
                "WHERE [dbo].[Inventory].[ProductId] IS IN @Param3",
                query.CommandText);
Update a record

The example below will generate a command to update the User table, provides 3 properties to be updated, where user.Id equals the given value userId

var query = SqlBuilder.Update<User>(_ => new User
                                   {
                                       Email              = _.Email.Replace("@domain1.com", "@domain2.com"),
                                       LastChangePassword = DateTimeOffset.Now,
                                       FailedLogIns       = _.FailedLogIns + 1
                                   })
                    .Where(user => user.Id == userId);

var result = Connection.Execute(query.CommandText, query.CommandParameters);
// this will return the affected rows of the query
Delete a record / multiple records by condition

The example below will generate a command to delete from User table where the user.Email equals the specified userEmail value:

string userEmail = "query_email@domain1.com";

var query = SqlBuilder.Delete<User>()
                    .Where(user => user.Email == userEmail);
                    // .Where(user => user.Email.Contains("part_of_email_to_search"));

var result = Connection.Execute(query.CommandText, query.CommandParameters);

Reference

https://github.com/DomanyDusan/lambda-sql-builder

https://github.com/mladenb/sql-query-builder

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DotNetBrightener.LinQToSqlBuilder:

Package Downloads
DotNetBrightener.LinQToSqlBuilder.Mssql

Microsoft SQL Server provider for LinQToSqlBuilder. Enables converting LINQ lambda expressions into T-SQL queries with SQL Server-specific syntax.

DotNetBrightener.LinQToSqlBuilder.Npgsql

PostgreSQL (Npgsql) provider for LinQToSqlBuilder. Enables converting LINQ lambda expressions into PostgreSQL queries with database-specific syntax.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.0.1-preview-net10-upgr... 153 11/25/2025
2026.0.1-preview-net10-upgr... 148 11/25/2025
2026.0.1-preview-net10-upgr... 148 11/25/2025
2026.0.1-preview-net10-upgr... 154 11/24/2025
2026.0.1-preview-net10-upgr... 156 11/24/2025
2026.0.1-preview-net10-upgr... 160 11/24/2025
2026.0.1-preview-669 0 11/29/2025
2026.0.1-preview-667 105 11/27/2025
2026.0.1-preview-665 115 11/27/2025
2026.0.1-preview-664 114 11/27/2025
2026.0.1-preview-660 147 11/25/2025
2025.0.10 154 11/25/2025
2025.0.10-preview-628 376 11/20/2025
2025.0.10-preview-605 255 11/17/2025
2025.0.10-preview-602 262 11/17/2025
2025.0.10-preview-581 251 11/11/2025
2025.0.10-preview-559 178 10/29/2025
2025.0.10-preview-557 163 10/27/2025
2025.0.9 168 10/26/2025
2025.0.9-preview-553 160 10/26/2025
2025.0.9-preview-539 103 10/12/2025
2025.0.9-preview-535 161 10/8/2025
2025.0.9-preview-509 176 10/2/2025
2025.0.9-preview-508 167 10/2/2025
2025.0.9-preview-487 168 9/29/2025
2025.0.9-preview-481 158 9/29/2025
2025.0.9-preview-473 141 9/28/2025
2025.0.8 166 9/23/2025
2025.0.6 169 9/22/2025
2025.0.6-preview-455 175 9/22/2025
2025.0.6-preview-454 301 9/17/2025
2025.0.6-preview-441 141 9/14/2025
2025.0.6-preview-440 137 9/14/2025
2025.0.6-preview-406 169 9/2/2025
2025.0.6-preview-401 166 9/2/2025
2025.0.6-preview-400 168 9/2/2025
2025.0.6-preview-369 213 8/27/2025
2025.0.6-preview-368 213 8/27/2025
2025.0.6-preview-334 164 8/18/2025
2025.0.6-preview-333 124 7/27/2025
2025.0.6-preview-332 498 7/24/2025
2025.0.6-preview-331 499 7/24/2025
2025.0.6-preview-328 500 7/24/2025
2025.0.6-preview-327 343 7/21/2025
2025.0.6-preview-326 343 7/21/2025
2025.0.6-preview-325 253 7/20/2025
2025.0.6-preview-324 258 7/20/2025
2025.0.6-preview-322 257 7/20/2025
2025.0.6-preview-321 253 7/19/2025
2025.0.6-preview-320 254 7/19/2025
2025.0.6-preview-319 163 7/17/2025
2025.0.6-preview-317 178 7/17/2025
2025.0.6-preview-316 161 7/17/2025
2025.0.6-preview-315 162 7/17/2025
2025.0.6-preview-314 161 7/17/2025
2025.0.6-preview-313 158 7/17/2025
2025.0.6-preview-312 163 7/16/2025
2025.0.5 175 7/10/2025
2025.0.5-preview-307 97 7/5/2025
2025.0.4 100 7/5/2025
2025.0.4-preview-305 111 7/4/2025
2025.0.4-preview-304 168 7/1/2025
2025.0.4-preview-299 93 5/31/2025
2025.0.4-preview-298 130 5/30/2025
2025.0.4-preview-296 158 5/30/2025
2025.0.4-preview-295 170 5/29/2025
2025.0.4-preview-293 167 5/26/2025
2025.0.4-preview-292 173 5/26/2025
2025.0.3 155 2/10/2025
2025.0.3-preview-288 136 2/10/2025
2025.0.2 148 1/21/2025
2025.0.2-preview-278 124 1/21/2025
2025.0.2-preview-277 141 12/16/2024
2025.0.1-rc-243301701 133 11/25/2024
2024.0.14.6 157 11/25/2024
2024.0.14.6-rc-243031001 128 10/29/2024
2024.0.14.6-rc-243030701 111 10/29/2024
2024.0.14.6-rc-242840501 125 10/10/2024
2024.0.14.6-rc-242820305 131 10/8/2024
2024.0.14.6-rc-242771401 113 10/3/2024
2024.0.14.6-rc-242770501 138 10/3/2024
2024.0.14.6-rc-242770201 120 10/3/2024
2024.0.14.6-rc-242761801 121 10/2/2024
2024.0.14.6-rc-242761601 123 10/2/2024
2024.0.14.6-rc-242761501 121 10/2/2024
2024.0.14.6-rc-242761401 125 10/2/2024
2024.0.14.6-rc-242760701 122 10/2/2024
2024.0.14.6-rc-242751002 131 10/1/2024
2024.0.14.6-rc-242750901 121 10/1/2024
2024.0.14.6-rc-242750502 137 10/1/2024
2024.0.14.6-rc-242750201 116 10/1/2024
2024.0.14.6-rc-242741501 123 9/30/2024
2024.0.14.6-rc-242730701 119 9/29/2024
2024.0.14.6-preview-2730501 136 9/29/2024
2024.0.14.6-preview-2701501 132 9/26/2024
2024.0.14.6-preview-2620901 131 9/18/2024
2024.0.14.6-preview-2570701 135 9/13/2024
2024.0.14.6-preview-2510703 146 9/7/2024
2024.0.14.6-preview-2480501 138 9/4/2024
2024.0.14.6-preview-2430401 136 8/30/2024
2024.0.14.6-preview-242730701 122 9/29/2024
2024.0.14.6-preview-2421703 125 8/29/2024
2024.0.14.6-preview-2421701 140 8/29/2024
2024.0.14.6-preview-2420901 131 8/29/2024
2024.0.14.6-preview-2390101 144 8/26/2024
2024.0.14.6-preview-2381603 148 8/25/2024
2024.0.14.6-preview-2341601 162 8/21/2024
2024.0.14.6-preview-2321602 157 8/20/2024
2024.0.14.6-preview-2190801 108 8/6/2024
2024.0.14.6-preview-2041501 146 7/22/2024
2024.0.14.6-preview-1920603 146 7/10/2024
2024.0.14.6-preview-1920301 139 7/10/2024
2024.0.14.6-preview-1911302 152 7/9/2024
2024.0.14.6-preview-1901001 136 7/8/2024
2024.0.14.6-preview-1900901 142 7/8/2024
2024.0.14.6-preview-1900801 136 7/8/2024
2024.0.14.6-preview-1860304 147 7/4/2024
2024.0.14.5 155 7/1/2024
2024.0.14.5-preview-1811601 153 6/29/2024
2024.0.14.5-preview-1810501 138 6/29/2024
2024.0.14.5-preview-180132 147 6/28/2024
2024.0.14.5-preview-180131 153 6/28/2024
2024.0.14.5-preview-180121 133 6/28/2024
2024.0.14.4 163 6/27/2024
2024.0.14.4-preview-7 144 6/27/2024
2024.0.14.3 156 6/21/2024
2024.0.14.1 158 6/6/2024
2024.0.14.1-preview 126 6/6/2024
2024.0.14-preview-1 126 6/6/2024
2024.0.13.8-preview 129 6/6/2024
2024.0.13.1-preview-0146 141 6/6/2024
2024.0.12.15803-preview-03 136 6/6/2024
2024.0.12.15608 162 6/4/2024
2024.0.12.15515 174 6/3/2024
2024.0.12.15220 153 5/31/2024
2024.0.12.15220-alpha31-240... 138 5/31/2024
2024.0.12.14911 157 5/28/2024
2024.0.12.14910-alpha28-240... 143 5/28/2024
2024.0.12.14823 160 5/27/2024
2024.0.12.14522-alpha7-2405... 150 5/24/2024
2024.0.12.14514-alpha6-2405... 160 5/24/2024
2024.0.12.14511 157 5/24/2024
2024.0.12.14314 179 5/22/2024
2024.0.12.14114 162 5/20/2024
2024.0.12.12815 163 5/7/2024
2024.0.12.12814 166 5/7/2024
2024.0.12.12721 172 5/6/2024
2024.0.12.12702 189 5/5/2024
2024.0.12.12622 178 5/5/2024
2024.0.12.12514 171 5/4/2024
2024.0.12.12512 162 5/4/2024
2024.0.12.12510 169 5/4/2024
2024.0.12.12420 133 5/3/2024
2024.0.12.12319 137 5/2/2024
2024.0.12.12319-rc-2405021801 120 5/2/2024
2024.0.12.12318 126 5/2/2024
2024.0.12.12215 155 5/1/2024
2024.0.12.12011 163 4/29/2024