Skip to content

Joining

Wouter Demuynck edited this page Jun 12, 2016 · 4 revisions

By using the Join, LeftJoin, InnerJoin or RightJoin methods you can join tables like you would when writing SQL.

For example, the following code will generate a SQL snippet to select a blog post and its comments.

var sql = Sql
    .Select(p + "Id", p + "Title", c + "Comment", c + "CreatedBy", c + "CreatedOn")
    .From(p)
    .LeftJoin(c)
    .On(SqlExpression.Equal(c + "PostId", p + "Id"))
    .Where(SqlExpression.Equal(p + "Id", 42))
    .ToSql();

And the output of this code will be:

SELECT [p].[Id], [p].[Title], [c].[Comment], [c].[CreatedBy], [c].[CreatedOn] 
FROM [Post] [p] 
LEFT JOIN [Comment] [c] ON [c].[PostId] = [p].[Id] 
WHERE [p].[Id] = 42
Clone this wiki locally