![](/img/trans.png)
[英]How to write the Cosmos DB query with a 'in' keyword using LINQ?
[英]Cosmos DB Linq query always returns 0 results
我有一個具有以下結構的簡單文檔:
Id:guid id
FamilyName:"some name"
Children:[
{
name:"somename",
age:agevalue
}]
我的 sql 查詢基於以下語句返回一行數據:
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();
但是當我嘗試使用 linq 時,我總是得到零結果。 我對 linq 使用以下內容:
var x = client.CreateDocumentQuery<FamilyModel>(docColUri, option)
.SelectMany(f=>
f.Children.Where(c=> c.name == "somename"))
.ToList();
誰能幫助我,讓我知道我做錯了什么?
運行與您相同的查詢,我設法返回了結果。 這是我正在使用的文檔:
"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
我已經為 FamilyModel 設置了 class,如下所示:
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; }
}
為了運行查詢,我得到了以下代碼(簡單的 .NET 控制台應用程序)。
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);
}
}
}
我在控制台中得到以下結果返回給我:
您是否在選項中啟用了跨分區查詢? 我假設您的分區鍵是 /id 如果不是,請告訴我,我可以嘗試重新創建您的問題。
如果我在這里遺漏了您設置不同的內容,請告訴我,以便我可以重新創建您的場景。
希望這可以幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.