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
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!");
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.