Expression magic, how does it work? what gets evaluated at what time

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, 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?

    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?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum