Skip to content

Cosmos: Translate subquery in filter condition #17957

Closed

Description

@nse7ens

Consider this CosmosDb document structure:

{
    "Id": "f0609362-4008-4edc-ae1f-76ef1c0ef974",
    "Discriminator": "MyEntity",
    "Title": "Test entity 1",
    "id": "MyEntity|f0609362-4008-4edc-ae1f-76ef1c0ef974",
    "AuthorizedUsers": [
        {
            "Discriminator": "UserInfo",
            "DisplayName": "John Doe",
            "LoginName": "[email protected]"
        },
        {
            "Discriminator": "UserInfo",
            "DisplayName": "Jane Doe",
            "LoginName": "[email protected]"
        }
    ]
}

When querying data I would like to perform a query which filters on AuthorizedUsers properties. E.g.:

await _dbContext.MyEntities
                .Where(s => s.AuthorizedUsers.Any(m => m.LoginName == "[email protected]"))
                .ToListAsync();

However, this results in a query translation error:

System.InvalidOperationException: The LINQ expression 'Where(\r\n source: DbSet, \r\n predicate: (s) => Any(\r\n source: AsQueryable(Property<ICollection>(s, "AuthorizedUsers")), \r\n predicate: (o) => o.LoginName == "[email protected]"))' could not be translated. Either rewrite the query in a form that can be translated

Question: is this something that is or should be possible to achieve? The same query can be executed on CosmosDB SQL syntax, like this:

SELECT VALUE c
FROM c
WHERE EXISTS(
    SELECT VALUE u
    FROM u IN c.AuthorizedUsers
    WHERE u.LoginName = "[email protected]"
)

Activity

  1. smitpatel commented on Sep 20, 2019

    @smitpatel
    Contributor

    CosmosDb also supports Exists operator. I could not find it docs (hence it hasn't implemented yet) but query runs correctly.

  2. nse7ens commented on Sep 20, 2019

    @nse7ens
    Author

    @smitpatel you mean there is an undocumented way to achieve the same result using EF als linq? Or what do you mean exactly?

  3. added this to the Backlog milestone on Sep 20, 2019
  4. smitpatel commented on Sep 20, 2019

    @smitpatel
    Contributor

    I meant that it is not in cosmos docs so we did not know that such SQL was valid. Hence it does not work in EF Core yet.

  5. nse7ens commented on Sep 20, 2019

    @nse7ens
    Author
  6. smitpatel commented on Sep 20, 2019

    @smitpatel
    Contributor

    No work-around.

  7. AndriySvyryd commented on Sep 20, 2019

    @AndriySvyryd
    Member

    Related to #16146

  8. nse7ens commented on Sep 22, 2019

    @nse7ens
    Author

    @AndriySvyryd @smitpatel are there any other operators that do translate into a subquery (JOIN, SELECT, ...)?

  9. smitpatel commented on Sep 23, 2019

    @smitpatel
    Contributor

    @NickSevens - We don't have translation of subcollections composition in Cosmos yet. Mainly Exists operator or a in root.Orders pattern (which is used for join).

  10. changed the title [-]CosmosDB query nested data[/-] [+]Cosmos: Query nested data[/+] on Oct 22, 2019
  11. changed the title [-]Cosmos: Query nested data[/-] [+]Cosmos: Translate subquery in filter condition[/+] on Nov 27, 2019
  12. 31 remaining items

  13. modified the milestones: Backlog, 9.0.0 on Aug 14, 2024
  14. roji commented on Aug 14, 2024

    @roji
    Member

    I can confirm that the query types listed on this issue should be fully working on EF 9.0 (this is a specific case of querying over nested collections, #16926, #25364). If memory serves, preview.7 - to be released in the next few days - should already work, so once that's released can you please test with it and report back on this issue?

  15. hasmMarques commented on Aug 14, 2024

    @hasmMarques

    I can confirm that the query types listed on this issue should be fully working on EF 9.0 (this is a specific case of querying over nested collections, #16926, #25364). If memory serves, preview.7 - to be released in the next few days - should already work, so once that's released can you please test with it and report back on this issue?

    Hi Roji,

    These are excellent news.
    Yes, once EF9 is released, we will test this and report the outcome.
    Thank you for your support.

  16. modified the milestones: 9.0.0, on Aug 21, 2024
  17. modified the milestones: , 9.0.0 on Oct 12, 2024
  18. hasmMarques commented on Jan 10, 2025

    @hasmMarques

    Hi,

    I confirm that this issue is now fixed in EF9.

    Thank you for your support!

  19. roji commented on Jan 10, 2025

    @roji
    Member

    @hasmMarques great, thanks for confirming!

  20. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions