0

I'm doing a 'projection' as per (I think) the standard pattern (loosely based on Can I reuse code for selecting a custom DTO object for a child property with EF Core? and https://benjii.me/2018/01/expression-projection-magic-entity-framework-core/), and associating the mapping from underlying tables into my derived schema as per the below.

This did give me a problem because there is a circular reference, that made the evaluation of the query blow up with a stack overflow.

I have a fix, where i (effectively) can pass what fields are required into the Projection method.

it works!

but actually it isnt clear to me what is evaluated when (I didnt expect it to work).

Projection takes a parameter, a HashSet, which is outside the Expression<>? but its evaluated inside the Expression<>, so I assume that the Expression<> itself doesnt encode this logic, it just gets evaluated eagerly and the result is embedded in the expression?

class ES_PRODUCT
{
    public decimal? oid { get; set; }
    public ES_PRODUCT p_episode_series { get; set; } 
    public static Expression<Func<Psiproductpart, ES_PRODUCT>> Projection(HashSet<string> fields)
    {
        return x => new ES_PRODUCT()
        {
            oid = x.Oid,
            p_episode_series = fields.Contains("is_p_episode_series") ? 
                ExtensionsCore.Invoke(ES_PRODUCT.Projection(new HashSet<string>()), x.Psiprogrampart.PrgIdSeriesNavigation.ProductpartNavigation) 
                : null
        };
    }
}

ExtensionsCore.Invoke comes from LinqKit, though the question is really about C# and expressions.

I sort of assume that the expression is returned which means that at least the "fields.Contains("is_p_episode_series")" is evaluated at this point, and it becomes true of false, but is the inline if evaluated or embedded in the expression?

MrD at KookerellaLtd
  • 2,412
  • 1
  • 15
  • 17
  • No, that's not a standard pattern. That's a *very* bad practice mixing up entities, queries and information that's supposed to be hidden by the ORM. Projection is performed by LINQ's `Select`. That will be translated to SQL by EF Core. What SQL expression are you trying to emulate here? – Panagiotis Kanavos May 04 '22 at 09:44
  • Are you trying to include a property "dynamically"? In that case use a different expression based on your criteria, don't include the check inside the LINQ expression – Panagiotis Kanavos May 04 '22 at 09:47
  • Normally nothing contained in the expression tree is evaluated. And unknown methods cause runtime exception during the query translation. All this is except if you use some external expression preprocessing library (don't you?), in which case it depends of the library preprocessor implementation. So, what is the `ExtensionsCore`, where they come from? LinqKit or something? – Ivan Stoev May 04 '22 at 10:10
  • I've ammended the question to clarify the above questions though the question is about C# and expressions, i think captured variables outside of lambda will be evaluated and embedded. – MrD at KookerellaLtd May 04 '22 at 10:34

0 Answers0