Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in lambda expressions between full .NET framework and .NET Core

Is there a difference in the declaration of lambda expressions between the .NET Framework and .NET Core?

The following expressions compiles in .NET Core:

var lastShift = timeline.Appointments
                        .OfType<DriverShiftAppointment>()
                        .SelectMany(x => x.Activities.Where(x => !x.IsTheoretical))
                        .OrderByDescending(x => x.Start)
                        .FirstOrDefault();

But not in the .NET framework.

The problem in this expression is the following part

.SelectMany(x => x.Activities.Where(x => !x.IsTheoretical))

where x is declared twice (SelectMany and Where).

This is the error in the .NET Framework:

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

.NET framework

enter image description here

Reproducable example:

public class DemoClass
{
    public IList<int> Numbers { get; set; } = new List<int>();
}

class Program
{
    static void Main(string[] args)
    {
        var lst = new List<DemoClass>
        {
            new DemoClass
            {
                Numbers = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8 }
            },
            new DemoClass
            {
                Numbers = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8 }
            },
            new DemoClass
            {
                Numbers = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8 }
            }
        };

        var result = lst.SelectMany(x => x.Numbers.Where(x => x % 2 == 0))
                        .OrderByDescending(x => x);

        Console.WriteLine("Hello World!");
    }
}
like image 467
BendEg Avatar asked Mar 24 '21 09:03

BendEg


People also ask

What is the difference between .NET .NET core and .NET Framework?

NET Framework is a platform for . NET applications on Windows whereas, NET Core is the latest version of the . NET Framework which is a cross-platform and open-source framework optimized for modern app needs and developer workflows.

What are the most significant differences between .NET Framework and core?

Net Framework is used for the development of both desktop and web applications as well as it supports windows forms and WPF applications. . NET Core is packaged and installed independently of the underlying operating system as it is cross-platform.

What is lambda expression in .NET Framework?

You use a lambda expression to create an anonymous function. Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms: Expression lambda that has an expression as its body: C# Copy.

What is the difference between .NET 5.0 .NET core and .NET Framework?

NET Core is a new version of. NET Framework, which is a free, open-source, general-purpose development platform maintained by Microsoft. It is a cross-platform framework that runs on Windows, macOS, and Linux operating systems.

What is the difference between NET Core and NET Framework?

To put it simple .NET Core is the new cross-platform and fully open source version of .NET (.NET Framework isn't cross-platform nor is it open source) Show activity on this post. The .NET framework is a software development framework from Microsoft.

What is the difference between WinForms forms and NET Core?

Windows Forms and WPF applications are very well supported. .Net Core focuses more on Web, Windows Mobile, and Windows Store applications. Currently, it does not support Desktop application development. .Net Framework is packaged as a whole.

What is the current version of the NET Framework?

The current version of .NET Framework, 4.8, is supposed to be the last version of .NET Framework. There will be no more new versions of .NET Framework planned in the future. .NET Core Advantages. If you’re building a new application and have a choice between .NET Core and .NET Framework, .NET Core is the way to go.

What is the use of NET Core in web development?

.Net Framework is used for the development of both desktop and web applications as well as it supports windows forms and WPF applications. .NET Core is packaged and installed independently of the underlying operating system as it is cross-platform.


1 Answers

It is telling you that problem is this line:

.SelectMany(x => x.Activities.Where(x => !x.IsTheoretical))

Using the x twice confuses it, it is ambiguous, try this:

.SelectMany(x => x.Activities.Where(y => !y.IsTheoretical))

But you are right, it compiles in core but not framework. It looks to be like this: https://github.com/dotnet/roslyn/issues/38377. Reading that link, it looks like this is a change in C# 8.0, and core is targeting 8.0 while framework is targeting 7.2.

like image 106
mikelegg Avatar answered Oct 08 '22 20:10

mikelegg