C# 如何有效地使用LINQ对Azure Cosmos文档DB SQL API执行动态查询?
我知道Azure Cosmos DB SQL API允许使用SQL语法从中查询文档并为我工作 例如:C# 如何有效地使用LINQ对Azure Cosmos文档DB SQL API执行动态查询?,c#,linq,dynamic,azure-cosmosdb,C#,Linq,Dynamic,Azure Cosmosdb,我知道Azure Cosmos DB SQL API允许使用SQL语法从中查询文档并为我工作 例如:SELECT*FROM c,其中类似“%PORT%”的c.DynamicContent.MailingAddress.City运行良好 我试图保存的数据如下所示: { "Id": 1001, "AttentionName": "Keyword list", "DynamicContent": { &
SELECT*FROM c,其中类似“%PORT%”的c.DynamicContent.MailingAddress.City
运行良好
我试图保存的数据如下所示:
{
"Id": 1001,
"AttentionName": "Keyword list",
"DynamicContent": {
"Agency": 2,
"EnteredOn": "2001-03-30T16:20:00+0000",
"UpdatedOn": "2001-03-30T16:20:00+0000",
"Name": "Sample Name",
"MailingAddress": {
"Address1": "501 Abbey St",
"City": "Portland",
"StateProvince": "OR",
"PostalCode": "97215",
"Country": "United States"
}
}
}
query.Where(c => c.DynamicContent.MailingAddress.City.Contains("PORT")).ToList();
由于DynamicContent中的内容是非结构化和动态的,因此我使用以下类来保存数据:
public partial class CosmosDocument
{
[JsonProperty("Id")]
public long Id { get; set; }
[JsonProperty("AttentionName")]
public string AttentionName { get; set; }
[JsonProperty("DynamicContent")]
public dynamic DynamicContent { get; set; }
}
我面临的问题是无法执行以下操作:
{
"Id": 1001,
"AttentionName": "Keyword list",
"DynamicContent": {
"Agency": 2,
"EnteredOn": "2001-03-30T16:20:00+0000",
"UpdatedOn": "2001-03-30T16:20:00+0000",
"Name": "Sample Name",
"MailingAddress": {
"Address1": "501 Abbey St",
"City": "Portland",
"StateProvince": "OR",
"PostalCode": "97215",
"Country": "United States"
}
}
}
query.Where(c => c.DynamicContent.MailingAddress.City.Contains("PORT")).ToList();
因为过滤器中可能出现的属性(动态和UI控制)位于动态类中,并且不是强类型的
有没有办法做到这一点,或者至少有其他解决办法来实现这一点?
请帮助您的思路。/P>>P>而不是使用LINQ进行查询,而是考虑使用,允许从任意字符串和参数值构造。例如,有以下示例:
QueryDefinition queryDefinition = new QueryDefinition("select * from ToDos t where t.cost > @expensive")
.WithParameter("@expensive", 9000);
using (FeedIterator<ToDoActivity> feedIterator = this.Container.GetItemQueryIterator<ToDoActivity>(
queryDefinition,
null,
new QueryRequestOptions() { PartitionKey = new PartitionKey("Error")}))
{
while (feedIterator.HasMoreResults)
{
foreach(var item in await feedIterator.ReadNextAsync())
{
Console.WriteLine(item.cost);
}
}
}
QueryDefinition QueryDefinition=newquerydefinition(“从ToDos t中选择*,其中t.cost>@priced”)
.带参数(“@昂贵”,9000);
使用(FeedIterator FeedIterator=this.Container.GetItemQueryIterator)(
查询定义,
无效的
新建QueryRequestOptions(){PartitionKey=new PartitionKey(“错误”)})
{
while(feedIterator.HasMoreResults)
{
foreach(wait feedIterator.ReadNextAsync()中的var项)
{
控制台写入线(项目成本);
}
}
}
您尝试过对象表示法吗?类似于c.DynamicContent[“MailingAddress”][“City”]。Container(“PORT”)
?请参阅此博客帖子:。是,已尝试。这是一个错误。