Generic Filtering with Ormlite

public IEnumerable<SRequest> Search(string postCode)
{
List<SRequest> data;
var dbFactory = new OrmLiteConnectionFactory(
DBConnection,
SqlServerOrmLiteDialectProvider.Instance);
using (var db = dbFactory.OpenDbConnection())
{
data = db.Select<SRequest>(x => x.PostCode == postCode);
}return data.AsEnumerable();
}
public IEnumerable<SRequest> Search(int caseReference)
{
List<SRequest> data;
var dbFactory = new OrmLiteConnectionFactory(
DBConnection,
SqlServerOrmLiteDialectProvider.Instance);
using (var db = dbFactory.OpenDbConnection())
{
data = db.Select<SRequest>(x => x.CaseReference == caseReference);
}return data.AsEnumerable();
}
var filter = new Func<SRequest, bool>(x => x.PostCode == postCode);
private List<SRequest> Query(Func<SRequest,bool> filter)
{
var dbFactory = new OrmLiteConnectionFactory(
DBConnection,
SqlServerOrmLiteDialectProvider.Instance);
IEnumerable<SRequest> data;using (var db = dbFactory.OpenDbConnection())
{
data = db.Select<SRequest>().Where(filter);
Log.Debug($@"{db.GetLastSql()}");
}
return data.ToList();
}
db.GetLastSql()
public IEnumerable<SRequest> Search(string postCode)
{
var filter = new Func<SRequest, bool>(x => x.PostCode == postCode);
return Query(filter).AsEnumerable();
}
public IEnumerable<SRequest> Search(int caseReference)
{
var filter = new Func<SRequest, bool>(x => x.CaseReference == caseReference);
return Query(filter).AsEnumerable();}private List<SRequest> Query(Func<SRequest,bool> filter)
{
var dbFactory = new OrmLiteConnectionFactory(
DBConnection,
SqlServerOrmLiteDialectProvider.Instance);
IEnumerable<SRequest> data;using (var db = dbFactory.OpenDbConnection())
{
data = db.Select<SRequest>().Where(filter);
}return data.ToList();
}

Async Querying

Converting this to an Async Query proved more tricky than expected. My first attempt at a QueryAsync method looked something like this

private async Task<List<SRequest>> QueryAsync(Func<SRequest,bool> filter)
{
var dbFactory = new OrmLiteConnectionFactory(
DBConnection,
SqlServerOrmLiteDialectProvider.Instance);
IEnumerable<SRequest> data;using (var db = dbFactory.OpenDbConnection())
{
data = await db.SelectAsync<SRequest>().Where(filter);
}return data.ToList();
}
Expression<Func<SRequest, bool>> filter = x => x.PostCode == postCode;
private async Task<List<SRequest>> QueryAsync(Expression<Func<SRequest, bool>> filter)
{
var dbFactory = new OrmLiteConnectionFactory(
DBConnection,
SqlServerOrmLiteDialectProvider.Instance);
List<SRequest> data;
using (var db = dbFactory.OpenDbConnection())
{
data = await db.SelectAsync(filter).ConfigureAwait(false);
}return data.ToList();
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Steve Ellwood

Steve Ellwood

Senior Integrations Officer at Doncaster Council Any views expressed are entirely my own.