In our application, we want to have standard methods for various conditions in our database. For example, we have different types of transactions, and we want to create standard methods for searching them in other queries. However, this gives us an error:
Method '' does not support SQL translation
The method might look like this:
public static bool IsDividend(this TransactionLog tl) { return tl.SourceTypeID == (int)JobType.Dividend || tl.SourceTypeID == (int)JobType.DividendAcct; }
Used as such:
var dividends = ctx.TransactionLogs.Where(x => x.IsDividend());
Of course, if I copy the logic from IsDividend() to the Where clause, this works fine, but I end up duplicating this logic in many places and it's hard to track if that logic changes.
I think that if I converted it to an expression like this, it would work, but it is not so preferable, since the installation is able to use methods:
public Expression<Func<TransactionLog, bool>> IsDividend = tl => tl.SourceTypeID == (int)JobType.Dividend || tl.SourceTypeID == (int)JobType.DividendAcct; var dividends = ctx.TransactionLogs.Where(IsDividend);
Is there a way to get Linq to evaluate a method as an expression? Or "convert" a method call to an expression in a linq query? Something like that:
var dividends = ctx.TransactionLogs.Where(tl => ToExpression(tl.IsDividend));
We use Linq-to-SQL in our application.