简体   繁体   中英

Cosmos DB Linq query always returns 0 results

i have a simple document with the following structure:

Id:guid id
FamilyName:"some name"
Children:[
{
  name:"somename",
  age:agevalue

}]

my sql query based following statement returns one row of data:

SqlQuerySpec query = new SqlQuerySpec(@"SELECT f  FROM Families f
                                           JOIN c IN f.Children
                                           WHERE c.name='somename'");               

var test = client.CreateDocumentQuery<FamilyModel>(docColUri, query, option).ToList();

but when i try to use linq i always get zero result. I am using the following for linq:

 var x = client.CreateDocumentQuery<FamilyModel>(docColUri, option)
                                      .SelectMany(f=>
                                          f.Children.Where(c=> c.name == "somename"))
                                           .ToList();

Can anyone please help me and let me know what am i doing wrong?

Running the same query as you, I've managed to return a result. Here is the document that I am working with:

"id": "1",
"FamilyName": "Smith",
"Children": [
    {
        "name": "John",
        "age": 21
    }
],
"_rid": "Gj0yANGrYVIDAAAAAAAAAA==",
"_self": "dbs/Gj0yAA==/colls/Gj0yANGrYVI=/docs/Gj0yANGrYVIDAAAAAAAAAA==/",
"_etag": "\"e6015c65-0000-0800-0000-5e94d6380000\"",
"_attachments": "attachments/",
"_ts": 1586812472

I've set up a class for FamilyModel like so:

public class FamilyModel
{
    public string id { get; set; }
    public string FamilyName { get; set; }
    public Children[] Children { get; set; }
}

public class Children
{
    public string name { get; set; }
    public int age { get; set; }
}

And to run the query, I've got the following code (Simple .NET Console application).

static void Main(string[] args)
    {
        Uri collectionLink = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);

        var option = new FeedOptions()
        {
            EnableCrossPartitionQuery = true
        };

        using (client = new DocumentClient(new Uri(endpoint), primaryKey))
        {

            var x = client.CreateDocumentQuery<FamilyModel>(collectionLink, option)
                .SelectMany(f => f.Children.Where(c => c.name == "John"))
                .ToList();

            foreach (var result in x)
            {
                Console.WriteLine(result.name);
                Console.WriteLine(result.age);
            }
        }
    }

And I get the following result returned back to me in the Console:

在此处输入图像描述

Have you enabled Cross Partition Queries in your options? I'm assuming that your partition key is /id so if it's not, let me know and I can attempt to recreate your problem.

If I've missed something here that you've set up differently, let me know so I can recreate your scenario.

Hope this helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM