title: Difference in lambda expressions between full .NET framework and .NET Core
body: <p>Is there a difference in the declaration of lambda expressions between the .NET Framework and .NET Core?</p>
  <p>The following expressions compiles in .NET Core:</p>
  <pre><code>var lastShift = timeline.Appointments
                          .OfType&lt;DriverShiftAppointment&gt;()
                          .SelectMany(x =&gt; x.Activities.Where(x =&gt; !x.IsTheoretical))
                          .OrderByDescending(x =&gt; x.Start)
                          .FirstOrDefault();
  </code></pre>
  <p>But not in the .NET framework.</p>
  <p>The problem in this expression is the following part</p>
  <pre><code>.SelectMany(x =&gt; x.Activities.Where(x =&gt; !x.IsTheoretical))
  </code></pre>
  <p>where <code>x</code> is declared twice (<code>SelectMany</code> and <code>Where</code>).</p>
  <p>This is the error in the .NET Framework:</p>
  <blockquote>
  <p>A local or parameter named 'x' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter</p>
  </blockquote>
  <p>.NET framework</p>
  <p><a href="https://i.stack.imgur.com/dCS9E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dCS9E.png" alt="enter image description here" /></a></p>
  <p>Reproducable example:</p>
  <pre><code>public class DemoClass
  {
      public IList&lt;int&gt; Numbers { get; set; } = new List&lt;int&gt;();
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          var lst = new List&lt;DemoClass&gt;
          {
              new DemoClass
              {
                  Numbers = new List&lt;int&gt;{ 1, 2, 3, 4, 5, 6, 7, 8 }
              },
              new DemoClass
              {
                  Numbers = new List&lt;int&gt;{ 1, 2, 3, 4, 5, 6, 7, 8 }
              },
              new DemoClass
              {
                  Numbers = new List&lt;int&gt;{ 1, 2, 3, 4, 5, 6, 7, 8 }
              }
          };
  
          var result = lst.SelectMany(x =&gt; x.Numbers.Where(x =&gt; x % 2 == 0))
                          .OrderByDescending(x =&gt; x);
  
          Console.WriteLine(&quot;Hello World!&quot;);
      }
  }
  </code></pre>