Dynamic Linq expressions in OrmLite

I know this is probably more my lack of knowledge of dotnet than something about OrmLite, but I have a number of table based operations that I’d like to abstract and they look something like this:

var cntSomethingFortheDay = Db.Scalar<SomeClass, int>(x => Sql.Count(x.SomeProperty), x => x.SomeDate >= DateTime.Now.AddDays(-1) && x.SomeDate <= DateTime.Now);

I know how to use generics to pass in an arbitrary “SomeClass”, but is it possible to pass in “SomeProperty” and “SomeDate” as well? (In this case, different classes have different date properties. I’d prefer not to implement interfaces if not necessary). As an alternative, I could also pass in the filter expression itself, but I do know how to craft a stand alone expression outside of the orm lite methods.

It would require manually building up the same Expression tree that the compiler would’ve generated which can be difficult to do dynamically and very fragile if you get anything wrong, e.g. using wrong type or cast.

If typed static expressions are ok, you can extract the filter into a variable that you can pass in as an argument, eg:

var fieldName = nameof(SomeClass.SomeDate);
Expression<Func<SomeClass, bool>> filter = x =>
    x.SomeDate >= DateTime.Now.AddDays(-1) && x.SomeDate <= DateTime.Now;
db.Scalar<SomeClass, int>(x => Sql.Count(fieldName), filter);

If you have JetBrains Rider you can also select the entire expression and extract it into a variable with CTRL+R,V.

Alternatively to dynamically create more complex expressions I’d suggest looking at building an SQL Expression instead, e.g:

var fieldName = nameof(SomeClass.SomeDate);
var q = db.From<SomeClass>();
var column = q.Column<SomeClass>(fieldName);
var count = db.Scalar<int>(q.Where(column + " >= {0} AND " + column + " < {1}",
        DateTime.Now.AddDays(-1), DateTime.Now)

Thank you, perfect static expressions will work just fine in my use case!