1

I'm trying to follow the solution presented here, but still no success: This query returns null:

  var plan = _dbClient.CreateDocumentQuery<Plan>(_collectionUri)
            .SelectMany(p => p.Users.Where(u => u.Id == userId)
            .Select(u => p))
            .AsEnumerable()
            .FirstOrDefault();

Although this returns the document I'm searching for (I'm trying to not leave the AsEnumerable before the Where:

  var plan = _dbClient.CreateDocumentQuery<Plan>(_collectionUri)
            .AsEnumerable() 
            .Where(p => p.Users.Any(u => u.Id == userId))
            .FirstOrDefault();

This is how the document looks like:

{
"id": "9710ef23-aae5-4215-a45b-641185140722",
"title": "Free Plan",
"planId": "freeplan",
"ranking": 1,
"description": "Most basic features",
"startFrom": "2018-01-01",
"monthlyCost": 0,
"details": [
  // supressed
],
"users": [
    {
        "id": "11111111-2222-333-4444-5555555555555",
        "enrolledIn": "2017-07-30 08:00:01"
    },
    {
        "id": "4760c977-6bcf-497d-96eb-c43e5bcf1ab9",
        "enrolledIn": "1981-07-05 08:00:01"
    }
],

}

Thank you in advance!

huysmania
  • 1,054
  • 5
  • 11
luizs81
  • 397
  • 2
  • 12
  • So, does `userId` equal any record's `Id`, case and all? – CodeCaster Jun 11 '18 at 09:00
  • Don't use the SDK like that. Synchronous LINQ calls will send many paged requests over the wire and block your thread. Use the .ExecuteAsync method instead. [Cosmonaut](https://github.com/Elfocrash/Cosmonaut) explains how to do that. – Nick Chapsas Jun 11 '18 at 12:18
  • @CodeCaster that was a good insight. I fixed the problem renaming the `id` in `User` by `UserId`. That `id` is actually coming from an external agent, and calling it `Id` definitely caused some confusion when making the query. – luizs81 Jun 12 '18 at 00:36
  • @NickChapsas I see that you are promoting the library you created. It looks nice, but this project is really small and I would like to keep it "plain" (without external libraries) unless strictly necessary. I might use in the future. – luizs81 Jun 12 '18 at 00:37
  • 1
    @luizs81 It has the same dependencies as the library you are referencing (which is an external library), but yeah, just don't do `.FirstOrDefault()` or `ToList()` on `CreateDocumentQuery`'s `IQueryable` because it's a pretty big mistake. – Nick Chapsas Jun 12 '18 at 13:36

1 Answers1

0

Firstly, I tested your first LINQ query and it works well for me.

enter image description here

You need to check if the case issue in your object corresponds to LINQ.

enter image description here

Secondly, I tested your second LINQ query and leave the AsEnumerable before the Where. It occurred errors: Method 'Any' is not supported.

Please check the LINQ to SQL API, I suggest you make sql with ARRAY_CONTAINS.

Hope it helps you.

luizs81
  • 397
  • 2
  • 12
Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • I fixed the problem renaming the `id` in `User` by `UserId`. That `id` is actually coming from an external agent, and calling it `Id` definitely caused some confusion when making the query. (I thought that not inheriting from `Document` could be the issue, but it's not related). About the error in `Any`, this is really weird, as once you converted to Enumerable, you should have access to all Linq extensions, event the ones not supported by DocumentDb – luizs81 Jun 12 '18 at 00:32
  • @luizs81 You find Any in the above LINQ support list? – Jay Gong Jun 12 '18 at 01:54
  • Yes, as far as I can understand. For example, if you create a `List` of objects, you can use `Any()` to query over the objects in the list. – luizs81 Jun 12 '18 at 04:47