2

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 ANDs and ORs 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.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
  • 1
    Can you give an example? We have created extension methods on `IQueryable` successfully without any hiccups. Maybe yours are more complicated, but I'd be suprised if it wouldn't work if the query as-is works. – Fildor May 03 '23 at 06:56
  • 1
    ^^ Example doesn't have to be actually from your code (if you cannot disclose). Just a [mcve]. – Fildor May 03 '23 at 06:57
  • 1
    You say you're using LINQ to SQL but you have tagged the question EF Core. Which is it? The two are mutually exclusive. The LINQ provider for EF is LINQ to Enties. – jmcilhinney May 03 '23 at 07:20
  • Does this answer your question? [Entity Framework Core - Use Extension Methods Inside Queryable](https://stackoverflow.com/questions/54512134/entity-framework-core-use-extension-methods-inside-queryable) – marsze May 03 '23 at 07:22
  • Check this [my answer](https://stackoverflow.com/a/70120616/10646316) It shows how to use LINQKit for such purpose. – Svyatoslav Danyliv May 03 '23 at 07:25
  • @Fildor Example given :-) – Dee J. Doena May 04 '23 at 08:41
  • @jmcilhinney Sorry, about the unclear terminology. I hope the added example will make it clearer. – Dee J. Doena May 04 '23 at 08:41

1 Answers1

0

I will give you a simple example on what I am using and based on it you can enhance it further:

  private Expression<Func<Tdb, bool>> BuildConditionForProperty(string propertyName, int conditionValue)
    {
        var parameterExpression = Expression.Parameter(typeof(Tdb));
        var constant = Expression.Constant(conditionValue);
        var property = Expression.Property(parameterExpression, propertyName);
        var condition = Expression.Equal(property, constant);
        return Expression.Lambda<Func<Tdb, bool>>(condition, parameterExpression);
    }//BuildConditionForProperty

This function (BuildConditionForProperty) build is used for a simple comparison between a column value from db and a specific value. Starting from here you can add more Expression.Equal or Expression.Or to create your conditional expression.

I am using it like this.

 var condition = BuildConditionForProperty("Id", (int)typeof(Tdto).GetProperty("Id").GetValue(data));
 var records = await _persistenceService.FindByCondition(lambdaCondition);

I hope this is answer that you are looking for.

D A
  • 1,724
  • 1
  • 8
  • 19