I'm not sure that "inline function" is the correct terminology but here's what I am trying to achieve:
I sometimes have WHERE
conditions in a query where a number of AND
s and OR
s are combined to the get the desired result from the database.
In good Uncle Bob fashion I would prefer to group these sub clauses into named functions so that in six months from now I still understand what I was trying to achieve, especially if the clause needs to be adapted.
If this were plain LINQ-to-objects this would not pose a problem but with the expression tree of IQueryable
it can't translate the functions into SQL.
There was a complex answer from 2014 but I was wondering if something had changed in the years that would make it possible.
Taking the answer here into account I tried to convert:
string endField = "...";
query = query.Where(p => p.GS1.Any(
g => g.Header == Decoder.DateAndTimeOfProductionIdentifier
&& string.Compare(g.Content, endField) <= 0));
However, I don't understand how I would get the endField
parameter into this expression:
string endField = "...";
query = query.Where(Test());
private System.Linq.Expressions.Expression<Func<Packaging, bool>> Test()
{
return p => p.GS1.Any(
g => g.Header == Decoder.DateAndTimeOfProductionIdentifier
&& string.Compare(g.Content, endField) <= 0); //endField not defined here
}
Simply adding the string to the expression function
doesn't work because then it won't match the signature that the .Where
function is epecting.