Skip to content

Convert an Expression into a query string that works with Dapper.

License

Notifications You must be signed in to change notification settings

andygjp/ExpressionToSql

Repository files navigation

ExpressionToSql

Package Status
ExpressionToSql NuGet version
ExpressionToSql.Dapper NuGet version
AppVeyor

You can write a strongly typed query and have it converted to a query that Dapper understands. Like so:

var postcode = "BL";
var query = Sql.Select((Address x) => new { x.Id, x.Postcode }).Where(x => x.Id > 10 && x.Postcode != postcode).ToString();
// "SELECT a.[Id], a.[Postcode] FROM [dbo].[Address] AS a WHERE a.[Id] > 10 AND a.[Postcode] <> @postcode"

Or:

var query = Sql.Select((Address x) => 1, "TableName").Where(x => x.Id == 1001).ToString();
// "SELECT 1 FROM [dbo].TableName AS a WHERE a.[Id] = 1001

ExpressionToSql.Dapper

You can write the same strongly typed query and pass it directly to Dapper, like so:

IEnumerable<Person> person = await _conn.QueryAsync(Sql.Select((Person x) => x).Where(x => x.Id == 2));
// "SELECT a.[Id], a.[Name] FROM [dbo].[Person] AS a WHERE a.[Id] = 2"

TODO

  • Add support for joins