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

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