我有一个具有以下结构的简单文档:
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设置了一个类:
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 Console应用程序)。
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,所以如果不是,请告诉我,我可以尝试重新创建您的问题。
[如果我在这里错过了您设置不同的东西,请告诉我,以便我重新创建您的方案。
希望这会有所帮助:)