linq-to-sql - Cosmos DB Linq 查询始终返回 0 个结果
问题描述
我有一个具有以下结构的简单文档:
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 控制台应用程序)。
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 如果不是,请告诉我,我可以尝试重新创建您的问题。
如果我在这里遗漏了您设置不同的内容,请告诉我,以便我可以重新创建您的场景。
希望这可以帮助 :)
推荐阅读
- c# - 为什么 system.dattime 不能转换为 Timespan。?
- javascript - AngularJs 单选按钮捕获 ng-click 中的键码
- http - 如果我的网址中有 ./ 或 ../ 或 .../ 会发生什么?
- java - 从字符串中删除逗号和一些字符
- flutter - 列表视图项之间的 Flutter Hero 类似动画
- react-redux - 如何编写reducer/action creator 组合来更新按钮点击状态?
- java - 语法错误,为什么我会得到它们?
- excel - VBA:由于评论/注释而导致过滤缓慢
- php - 如何在php中获取特定div的HTML?
- android-studio - Android Studio Ctrl+F FIND 文本框内文本的字体类型