Package | Status |
---|---|
ExpressionToSql | |
ExpressionToSql.Dapper | |
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
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"
- Add support for joins