I am trying to write Linq data for SQL queries, and I found that I have to use expressions instead of lambdas.
If you do not, it will try to convert Linq directly to SQL and will not work.
Take an example of the main keys in the table. All names are called by something else, but I would like to write a standard Linq WHERE clause that uses an expression to select the correct field.
I wrote the following test code:
static void Main(string[] args)
{
using (var ss = new DataScope()
{
var db = DataScope.DatabaseConnection;
var pk = 2;
var sql = db.Beats.AsQueryable();
sql = sql.Where(Filter(pk));
var res = sql.ToList();
}
}
static Expression<Func<Beat, bool>> Filter(int pk)
{
return e => e.BeatID > pk;
}
This code is the main one, filters the filter (int) using PKID, and I can hide it until the general code, with more derived properties returning an expression for each table.
But this is a little limited, I would only like to define an expression that points to the PK field and derive additional expressions from it.
, PKGreaterThan PKEquals, .
, .
, , (PK, GUID, , ..)
, .
, ( ):
static Expression<Func<Beat, int>> PKExpression
{
get { return e => e.BeatID; }
}
static Expression<Func<Beat, bool>> SuperFilter(int pk)
{
var gt = Expression.GreaterThan(PKExpression, Expression.Constant(pk));
return e => gt;
}
Expression.GreaterThan Expression<Func<Beat, int>>