SqlKata - Dynamic Sql query builder for dotnet | Product Hunt

🎉 SqlKata is now on Product Hunt

Please upvote to support the product development

From

From a Table or View

The Query constructor takes an optional parameter to set the from clause

//:playground
new Query("Posts");

Or you can use the From method to set it

//:playground
new Query().From("Posts");
SELECT * FROM [Posts]

Alias

To alias the table you should use the as syntax

//:playground
new Query("Posts as p")
SELECT * FROM [Posts] AS [p]

From a Sub Query

You can select from a sub query by passing a Query instance to the From method or you can use the Lambda function overload.

//:playground
var fewMonthsAgo = DateTime.UtcNow.AddMonths(-6);
var oldPostsQuery = new Query("Posts").Where("Date", "<", fewMonthsAgo).As("old");

var query = new Query().From(oldPostsQuery).OrderByDesc("Date");
SELECT * FROM (SELECT * FROM [Posts] WHERE [Date] < '2017-06-01 6:31:26') AS [old] ORDER BY [Date] DESC

From a Raw expression

The FromRaw method let you write raw expressions.

for example in SqlServer you can use the TABLESAMPLE to get a 10% sample of the total rows in the comments table.

    var query = new Query().FromRaw("Comments TABLESAMPLE SYSTEM (10 PERCENT)")
SELECT * FROM Comments TABLESAMPLE SYSTEM (10 PERCENT)

Note: Remember you can use the [] to wrap identifiers word in your string

Heap apparel

THE APPAREL BRAND FOR DEVELOPERS

one email per month about tips & tricks, new features, and maybe community feedback