You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The value returned by AsQueryable().FirstOrDefault is different between Net5.0 and Net6.0 #65419
The value returned by AsQueryable().FirstOrDefault is different between Net5.0 and Net6.0
The first record is always returned in Net6.0
Reproduction Steps
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
public class prgram
{ static void Main()
{
var Entitys = new ObservableCollection<object>();
dynamic a = new System.Dynamic.ExpandoObject();
a.ID = Guid.Parse("dddcf959-04d8-0ad1-de83-3a020e5cd8b1");
a.Name = "a";
dynamic b = new System.Dynamic.ExpandoObject();
b.ID = Guid.Parse("dddcf959-04d8-0ad1-de83-3a020e5cd8b2");
b.Name = "b";
Entitys.Add(a);
Entitys.Add(b);
//this return is dddcf959-04d8-0ad1-de83-3a020e5cd8b1 ???????
//If you switch back to NET5.0, the return is dddcf959-04d8-0ad1-de83-3a020e5cd8b2
Console.WriteLine(Entitys.AsQueryable().FirstOrDefault(t => (t).GetProperty("ID") == Guid.Parse("dddcf959-04d8-0ad1-de83-3a020e5cd8b2")).GetProperty("ID"));
//this return is dddcf959-04d8-0ad1-de83-3a020e5cd8b2 OK
Console.WriteLine(Entitys.AsEnumerable().FirstOrDefault(t => (t).GetProperty("ID") == Guid.Parse("dddcf959-04d8-0ad1-de83-3a020e5cd8b2")).GetProperty("ID"));
}
}
public static class ZtxExtension
{
public static Guid GetProperty(this Object source, string Propert)
{
dynamic D = source;
switch (Propert)
{
default:
return D.ID;
break;
}
}
}
I can confirm this is a regression from .NET 5. Root cause is a corner case in EnumerableRewriter that fails to map back to the correct IEnumerable method overload. The regression was triggered by #48886 which added new overloads for the First/Last/SingleOrDefault methods. We should try to fix this.
Activity
zlatanov commentedon Feb 16, 2022
I can confirm that there is indeed a problem. Here is a much simpler repo:
In both cases we should have the console print
null, but instead when we're querying against theobjectlist, the first item is returned.It seems
LastOrDefaultdoesn'talso has this problem.eiriktsarpalis commentedon Feb 18, 2022
I can confirm this is a regression from .NET 5. Root cause is a corner case in EnumerableRewriter that fails to map back to the correct IEnumerable method overload. The regression was triggered by #48886 which added new overloads for the First/Last/SingleOrDefault methods. We should try to fix this.