Search code examples
c#linqquery-expressions

LINQ query expression translator?


I'm adding a LINQ interface to some custom objects, but the C# compiler fails on type inference. However, I can write the equivalent query using the raw extension methods and type inference succeeds, so I'm not sure how the compiler is translating the query expression into extension method calls.

Is there a tool or compiler flag so I can view what the compiler is generating from my query expression so I figure this out?

This code is in an open source project, so I can provide links to source if that's helpful. Slight variations on the type signatures of the extension methods avoid this type inference error, but these variants don't have the semantics I'm after.


Solution

  • Your query comprehension code is:

    from f1 in e1
    from f2 in e2
    from f3 in e3
    select f3
    

    Your method call code is:

    e1
    .SelectMany(f1 => e2)
    .SelectMany(f2 => e3), (f2, f3) => f3))
    

    The query translation proceeds as follows. First we deal with the first two from clauses:

    from f1 in e1
    from f2 in e2
    from f3 in e3
    select f3;
    

    This is translated into

    from x in ( e1 ) . SelectMany( f1 => e2 , ( f1 , f2 ) => new { f1 , f2 } )
    from f3 in e3
    select f3;
    

    Where "x" is a transparent identifier. Since none of e1, e2 or e3 consume any range variable, the fact that this is a transparent identifier is irrelevant; no further rewriting needs to be done to handle the transparent identifier semantics.

    That result is then transformed into

    ( ( e1 ) . SelectMany( f1 => e2 , ( f1 , f2 ) => new { f1 , f2 } ) ) 
    .SelectMany( x => e3 , ( x , f3 ) => f3 )
    

    We can eliminate some of those parentheses:

    e1 
    .SelectMany( f1 => e2 , ( f1 , f2 ) => new { f1 , f2 } ) ) 
    .SelectMany( x => e3 , ( x , f3 ) => f3 )
    

    Clearly this is rather different from the syntactic transformation you've done manually, which, recall, was

    e1
    .SelectMany(f1 => e2)
    .SelectMany(f2 => e3), (f2, f3) => f3))
    

    If you substitute in your e1, e2, e3 into the actual syntactic transformation above, does the resulting expression pass type inference?

    If it does not, then the question is "why not?" Either there's something wrong with your code, or something wrong with the type inferrer. If there's something wrong with the type inferrer, let me know.

    If it does, then the question is "what's wrong with the syntactic transformation pass"? If there's something wrong with the syntactic transformation pass, again, let me know.

    Thanks!