### Install SqlKata Packages Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Installs the core SqlKata package and the optional SqlKata.Execution package for query execution support using .NET CLI. ```sh dotnet add package SqlKata dotnet add package SqlKata.Execution # (optional) If you want the execution support ``` -------------------------------- ### Setup Database Connection and Compiler Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Initializes a database connection, a SQL compiler, and a QueryFactory instance for building and executing queries. QueryFactory is provided by the SqlKata.Execution package. ```csharp var connection = new SqlConnection("..."); var compiler = new SqlCompiler(); var db = new QueryFactory(connection, compiler); ``` -------------------------------- ### Retrieve Recent Books (Top 10) Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Fetches the 10 most recent books by ordering them in descending order based on the 'PublishedAt' column. ```csharp var recent = db.Query("Books").OrderByDesc("PublishedAt").Limit(10).Get(); ``` -------------------------------- ### Paginate Query Results Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Retrieves a paginated list of records from the 'Books' table, specifying the page size. The result includes a list of items and methods to navigate to the next page. ```csharp var page1 = db.Query("Books").Paginate(10); foreach(var book in page1.List) { Console.WriteLine(book.Name); } ... var page2 = page1.Next(); ``` -------------------------------- ### Join Tables and Select Specific Columns Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Performs a join between the 'Books' and 'Authors' tables on their respective 'Id' columns. It selects all columns from 'Books' and the 'Name' from 'Authors', aliasing it as 'AuthorName'. ```csharp var books = db.Query("Books") .Join("Authors", "Authors.Id", "Books.AuthorId") .Select("Books.*", "Authors.Name as AuthorName") .Get(); foreach(var book in books) { Console.WriteLine($"{book.Title}: {book.AuthorName}"); } ``` -------------------------------- ### Retrieve All Records from Table Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Fetches all records from a specified table using the QueryFactory. ```csharp var books = db.Query("Books").Get(); ``` -------------------------------- ### Include Related Author Information Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Retrieves books and includes related author information by joining with the 'Authors' table. Assumes a foreign key relationship (e.g., 'AuthorId' in Books). The result includes an 'Author' property on each book object. ```csharp var books = db.Query("Books") .Include(db.Query("Authors")) // Assumes that the Books table has an `AuthorId` column .Get(); ``` -------------------------------- ### Insert New Record Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Inserts a new record into the 'Users' table with specified name and country ID. ```csharp int affected = db.Query("Users").Insert(new { Name = "Jane", CountryId = 1 }); ``` -------------------------------- ### Retrieve Single Record by ID and Language Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Fetches a single record from the 'Books' table based on 'Id' and 'Lang' criteria. ```csharp var introToSql = db.Query("Books").Where("Id", 145).Where("Lang", "en").First(); ``` -------------------------------- ### Retrieve Published Books Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Fetches records from the 'Books' table, filtering for entries where 'IsPublished' is true. ```csharp var books = db.Query("Books").WhereTrue("IsPublished").Get(); ``` -------------------------------- ### Conditional Query Execution Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Applies a 'WhereIn' clause to the query only if a specific condition (e.g., 'isFriday') is met. This allows for dynamic query building. ```csharp var isFriday = DateTime.Today.DayOfWeek == DayOfWeek.Friday; var books = db.Query("Books") .When(isFriday, q => q.WhereIn("Category", new [] {"OpenSource", "MachineLearning"})) .Get(); ``` -------------------------------- ### Update Existing Records Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Updates records in the 'Users' table where 'Id' is 1, setting new values for 'Name' and 'CountryId'. ```csharp int affected = db.Query("Users").Where("Id", 1).Update(new { Name = "Jane", CountryId = 1 }); ``` -------------------------------- ### Delete Records Source: https://github.com/sqlkata/querybuilder/blob/main/README.md Deletes records from the 'Users' table where 'Id' is 1. ```csharp int affected = db.Query("Users").Where("Id", 1).Delete(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.