Skip to content

The value returned by AsQueryable().FirstOrDefault is different between Net5.0 and Net6.0  #65419

Closed

Description

@angtianqiang

Description

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;
        }
    }
}

Expected behavior

image

Actual behavior

image

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Feb 16, 2022
  2. zlatanov commented on Feb 16, 2022

    @zlatanov
    Contributor

    I can confirm that there is indeed a problem. Here is a much simpler repo:

    var a = new List<object>();
    var b = new List<string>();
    
    a.Add("test");
    b.Add("test");
    
    Console.WriteLine("a: " + a.AsQueryable().FirstOrDefault(x => false));
    Console.WriteLine("b: " + b.AsQueryable().FirstOrDefault(x => false));

    In both cases we should have the console print null, but instead when we're querying against the object list, the first item is returned.

    It seems LastOrDefault doesn't also has this problem.

  3. eiriktsarpalis commented on Feb 18, 2022

    @eiriktsarpalis
    Member

    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.

  4. added and removed
    untriagedNew issue has not been triaged by the area owner
    on Feb 18, 2022
  5. added this to the 6.0.x milestone on Feb 18, 2022
  6. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 18, 2022
  7. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 24, 2022
  8. ghost locked as resolved and limited conversation to collaborators on Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions