Skip to content

SingleOrDefault() with IncludeOptimized returns the first item for not single collection #822

Closed

Description

@magicmanam

Hi guys!

Is it expected that SingleOrDefault() method returns the first element for IQueryable object returned by IncludeOptimized WHEN collection contains more than one element? I didnt find anything about it on your site and was a little bit confused.

https://dotnetfiddle.net/qk5l1f

My expectation here is to see InvalidOperationException thrown.

Activity

  1. changed the title [-]SingleOrDefault() with IncludeOptimized returns the first item[/-] [+]SingleOrDefault() with IncludeOptimized returns the first item for not single collection[/+] on Dec 16, 2024
  2. JonathanMagnan commented on Dec 16, 2024

    @JonathanMagnan
    Member

    Hello @magicmanam ,

    Thank you for reporting.

    We will see if there is something we can do about it.

    Best Regards,

    Jon

  3. JonathanMagnan commented on Dec 22, 2024

    @JonathanMagnan
    Member

    Hello @magicmanam ,

    After investigating, due to how our library is made, we will not be able to support this combination. This is more complex as we don't control everything on our side, and a lot of logic is done on the EF side.

    You can somewhat use a trick on your side, but I don't recommend it even if it's working:

    var t = context.EntitySimples.IncludeOptimized(x => x.Child).Take(2).ToList().SingleOrDefault();

    Best Regards,

    Jon

  4. magicmanam commented on Dec 23, 2024

    @magicmanam
    Author

    Hello @JonathanMagnan! Thanks for the answer!

    Maybe it makes sence to add a remark to IncludeOptimized method's annotation and/or mention about this behavior in the documentation?

    Anyway, feel free to close this issue. Thanks!

  5. JonathanMagnan commented on Dec 24, 2024

    @JonathanMagnan
    Member

    Hello @magicmanam ,

    Thank you for your suggestion. I added a text about this in the limitations section and referenced this issue.

    Best Regards,

    Jon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions