4

The following is the simple form of SelectMany(). How if at all can we convert this into query syntax?

var array = new string[] { "Shaun", "Luttin" };
array
    .SelectMany(
        s => s
    );

The best that I can do produces the same output but introduces a new variable c...

var query = 
    from s in array.AsQueryable()
    from c in s
    select c;

...and results in the following fluent syntax.

array
   .SelectMany (
      s => s, 
      (s, c) => c
   );

Re: Possible Duplication

I have read the answers to Is there a C# LINQ syntax for the Queryable.SelectMany() method? I'm afraid the answers' translation's do not compile back to the original fluent syntax.

Community
  • 1
  • 1
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 4
    Query expressions have a fluent equivalent. But not all fluent method calls have a query comprehension that they round trip with. – Eric Lippert Apr 14 '15 at 23:33

1 Answers1

3

The compiler performs a translation to turn query syntax into method syntax. The details are specified in section 7.6.12 of the C# 5 spec. A quick search turns up only a couple translations that can result in a call to SelectMany, all in section 7.6.12.4:

A query expression with a second from clause followed by a select clause:
from x1 in e1
from x2 in e2
select v
is translated into ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )

and

A query expression with a second from clause followed by something other than a select clause:
from x1 in e1
from x2 in e2

is translated into
from * in ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => new { x1 , x2 } ) …

So there appears to be no translation that results in the other overload of SelectMany being called.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • apparently if the `...` includes `group` without a following `from` that will prevent `SelectMany` from being called. – NetMage Jan 05 '18 at 21:33