title: Nice, clean cross join in Linq using only extension methods
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/9115675/nested-from-linq-query-expressed-with-extension-methods">Nested &ldquo;from&rdquo; LINQ query expressed with extension methods</a>  </p>
  </blockquote>
  
  
  
  <p>I'm sure this has been asked before, but I honestly couldn't find anything.</p>
  
  <p>I'm curious what the equivalent syntax would be for the following using only built-in Linq extension methods:</p>
  
  <pre><code>var z1 =
      from x in xs
      from y in ys
      select new { x, y };
  </code></pre>
  
  <p>I can get the same results with this:</p>
  
  <pre><code>var z2 = xs.SelectMany(x =&gt; ys.Select(y =&gt; new { x, y }));
  </code></pre>
  
  <p>But it produces different IL code, and the code is a bit convoluted and hard to understand. Is there a cleaner way to do this with extension methods?</p>
  
  <hr>
  
  <p>Here's my entire test method as written:</p>
  
  <pre><code>private void Test()
  {
      var xs = new[] { 1D, 2D, 3D };
      var ys = new[] { 4D, 5D, 6D };
  
      var z1 =
          from x in xs
          from y in ys
          select new { x, y };
  
      var z2 = xs.SelectMany(x =&gt; ys.Select(y =&gt; new { x, y }));
  }
  </code></pre>
  
  <p>Here's the [Edit: C# interp of the] IL code (using ILSpy):</p>
  
  <pre><code>private void Test()
  {
      double[] xs = new double[]
      {
          1.0, 
          2.0, 
          3.0
      };
      double[] ys = new double[]
      {
          4.0, 
          5.0, 
          6.0
      };
      var z =
          from x in xs
          from y in ys
          select new
          {
              x = x,
              y = y
          };
      var z2 = xs.SelectMany((double x) =&gt;
          from y in ys
          select new
          {
              x = x,
              y = y
          });
  }
  </code></pre>