Skip to content

Getting Started

Marcus Ackre Medina edited this page Jul 20, 2026 · 2 revisions

Getting Started

Install

dotnet add package MarcusMedina.Fluent.Data.Sql

Requires .NET 10+

Basic SELECT

using MarcusMedina.Fluent.Data.Sql;

var sql = new Sql(DatabaseType.PostgreSQL)
    .Table("customers")
    .Select("id", "name", "email")
    .Build();
// SELECT "id", "name", "email" FROM "customers";

Select() is optional — omit it (or call it with no arguments) and you get SELECT *.

With WHERE and ORDER BY

var sql = new Sql(DatabaseType.PostgreSQL)
    .Table("orders")
    .Is("status", "pending")
    .When("total > 100")
    .OrderBy("created_at", asc: false)
    .Build();
// SELECT * FROM "orders" WHERE "status" = 'pending' AND total > 100 ORDER BY "created_at" DESC;

Sql doesn't take (column, operator, value) triples — each comparison has its own named method (.Is, .IsNot, .Contains, .Between, .In, .IsNull, ...). Use .When(rawCondition) for anything not covered by a named method.

OR conditions

Conditions are joined with AND by default. Call .Or() right before a condition to join it with OR instead:

var sql = new Sql(DatabaseType.PostgreSQL)
    .Table("users")
    .Is("role", "admin")
    .Or().Is("role", "owner")
    .Build();
// SELECT * FROM "users" WHERE "role" = 'admin' OR "role" = 'owner';

JOIN, LIMIT and OFFSET

var sql = new Sql(DatabaseType.PostgreSQL)
    .Table("orders")
    .Join("customers", "orders.customer_id = customers.id")
    .OrderBy("orders.created_at", asc: false)
    .Limit(10)
    .Offset(20)
    .Build();

Database-specific quoting

The constructor argument controls how table/column names are quoted:

new Sql(DatabaseType.MySQL).Table("users").Build();      // SELECT * FROM `users`;
new Sql(DatabaseType.SqlServer).Table("users").Build();   // SELECT * FROM [users];

What this package does not do

There is no .Insert(), .Update() or .Delete()Sql only builds SELECT statements. There is also no static FluentSql entry point; you always start with new Sql(dbType).

Clone this wiki locally