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]
To alias the table you should use the as
syntax
//:playground
new Query("Posts as p")
SELECT * FROM [Posts] AS [p]
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
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