ServiceStack: OrmLite SqlExpression Tip

When creating complicated clauses for queries use the underlying IDBconnection with the template type of the underlying source, but make sure to use the db.From() and then append the Where clauses afterwards in a series to generate the appropriate parameterized and checked clause.

Once the SqlExpression is built you can use it with a .Select(SqlExpression) to obtain the results.

Additional note extension methods do not filter in the parameters correct and lose scope and fail at runtime when creating a SqlExpression tree

var theQuery = Db.From<LocationRequest>()
	.Where(a => a.Start >= query.MinStart.ToBoundedSqlDateTime())
	.Where(b => b.Start <= query.MaxStart.ToBoundedSqlDateTime())
	.Where(c => c.End >= query.MinEnd.ToBoundedSqlDateTime())
	.Where(d => d.End <= query.MaxEnd.ToBoundedSqlDateTime())
	.Where(e => query.Deleted || e.Deleted == false)
	.Where(f => query.RequestedBy <= 0 || f.RequestedBy == query.RequestedBy)
	.Where(g => string.IsNullOrEmpty(query.Imei) || g.Imei == query.Imei)
	.Where(h => string.IsNullOrEmpty(query.Msisdn) || h.Msisdn == query.Msisdn);
var results = Db.Select(theQuery);
return results;