C# 带参数的Cosmosdb documentdb Sql查询返回0项
我有一个疑问。当我传递单个参数deviceId时,查询工作正常。如果添加其他两个参数,则即使集合已正确填充且具有满足所需条件的项,也不会向我返回任何项。 我附上代码c,它完成上述操作C# 带参数的Cosmosdb documentdb Sql查询返回0项,c#,sql,azure-cosmosdb,C#,Sql,Azure Cosmosdb,我有一个疑问。当我传递单个参数deviceId时,查询工作正常。如果添加其他两个参数,则即使集合已正确填充且具有满足所需条件的项,也不会向我返回任何项。 我附上代码c,它完成上述操作 public static async Task<IEnumerable<T>> GetItemsCompact(string deviceId,DateTime startdate,DateTime enddate) { string collec
public static async Task<IEnumerable<T>> GetItemsCompact(string deviceId,DateTime startdate,DateTime enddate)
{
string collectionToUse;
SqlQuerySpec sqlQuerySpec = new SqlQuerySpec();
if (typeof(T).ToString().IndexOf("Telemetry") != -1)
{
DocumentDBRepository<EBB.Web.Telemerty.Models.CosmosDBTelemetry>.Initialize();
collectionToUse = AppSettings.Collection;
sqlQuerySpec.QueryText = "SELECT c.messageUID as messageUID, ";
sqlQuerySpec.QueryText += "c.deviceId as deviceId, ";
sqlQuerySpec.QueryText += "udf.UDF_VIn(c.VIn) as VIn, ";
sqlQuerySpec.QueryText += "udf.UDF_AIn(c.AIn) as AIn, ";
sqlQuerySpec.QueryText += "udf.UDF_W(c.W) as W, ";
sqlQuerySpec.QueryText += "udf.UDF_Var(c.Var) as Var, ";
sqlQuerySpec.QueryText += "c.EventProcessedUtcTime as EventProcessedUtcTime ";
sqlQuerySpec.QueryText += "from Telemetry c ";
sqlQuerySpec.QueryText += "where c.deviceId = @deviceId and ";
sqlQuerySpec.QueryText += "c.EventProcessedUtcTime >= @startdate and ";
sqlQuerySpec.QueryText += "c.EventProcessedUtcTime <= @enddate";
sqlQuerySpec.Parameters = new SqlParameterCollection()
{
new SqlParameter("@deviceId", deviceId),
new SqlParameter("@startdate",startdate),
new SqlParameter("@enddate",enddate)
};
}
else
{
DocumentDBRepository<EBB.Web.Telemerty.Models.CosmosDBEvents>.Initialize();
collectionToUse = AppSettings.Collection2;
sqlQuerySpec.QueryText = "select doc.uid as uid, ";
sqlQuerySpec.QueryText += "doc.deviceId as deviceId, ";
sqlQuerySpec.QueryText += "doc.eventId as eventId, ";
sqlQuerySpec.QueryText += "doc.ts as ts, ";
sqlQuerySpec.QueryText += "doc.startTS as startTS, ";
sqlQuerySpec.QueryText += "doc.endTS as endTS, ";
sqlQuerySpec.QueryText += "doc.no as no, ";
sqlQuerySpec.QueryText += "doc.params as params, ";
sqlQuerySpec.QueryText += "doc.tags as tags ";
sqlQuerySpec.QueryText += "from Events doc ";
sqlQuerySpec.QueryText += "where doc.deviceId = @deviceId and ";
sqlQuerySpec.QueryText += "doc.EventProcessedUtcTime >= @startdate and ";
sqlQuerySpec.QueryText += "doc.EventProcessedUtcTime <= @enddate";
sqlQuerySpec.Parameters = new SqlParameterCollection()
{
new SqlParameter("@deviceId", deviceId),
new SqlParameter("@startdate",startdate),
new SqlParameter("@enddate",enddate)
};
}
FeedOptions opt = new FeedOptions
{
EnableCrossPartitionQuery = true,
MaxItemCount = -1
};
IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
UriFactory.CreateDocumentCollectionUri(AppSettings.Database, collectionToUse), sqlQuerySpec,
opt).AsDocumentQuery();
List<T> results = new List<T>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<T>());
}
return results;
}
遥测情况下的query.toSting等于:
"{\"query\":\"SELECT c.messageUID as messageUID, c.deviceId as deviceId, udf.UDF_VIn(c.VIn) as VIn, udf.UDF_AIn(c.AIn) as AIn, udf.UDF_W(c.W) as W, udf.UDF_Var(c.Var) as Var, c.EventProcessedUtcTime as EventProcessedUtcTime from Telemetry c where c.deviceId = @deviceId and c.EventProcessedUtcTime >= @startdate and c.EventProcessedUtcTime <= @enddate\",\"parameters\":[{\"name\":\"@deviceId\",\"value\":\"where c.deviceId = \\\"EBBBrain1874008291\\\"\"},{\"name\":\"@startdate\",\"value\":\"2019-06-01T00:00:00\"},{\"name\":\"@enddate\",\"value\":\"2019-06-30T00:00:00\"}]}"
有人能帮我吗?提前谢谢。Simone问题是由于参数中的属性序列化引起的。当您使用UTC创建文档时,但当您进行查询时,SDK认为DateTime的DateTimeKind未指定,这是有效的,因此它将其转换为未指定的格式2019-06-30T00:00:00,而不是UTC 2019-05-31T14:58:15.3238226Z。如果你使用了DateTime之类的东西,现在它将使用本地格式,看起来像这样的2009-06-15T13:45:30.0000000-07:00。无法返回任何结果的原因是,日期范围检查最终是一个字符串比较,由ISO格式启用 要使其正常工作,需要使用DateTime.SpecifyKind方法。以下是parameters子句的外观:
sqlQuerySpec.Parameters = new SqlParameterCollection()
{
new SqlParameter("@deviceId", deviceId),
new SqlParameter("@startdate", DateTime.SpecifyKind(startdate, DateTimeKind.Utc)),
new SqlParameter("@enddate", DateTime.SpecifyKind(enddate, DateTimeKind.Utc))
};
正在运行的参数是deviceId,日期是失败的吗?@Nick:是的,日期是失败的,deviceId正在运行。请在CreateDocumentQuery行之后进行查询。ToString调用?这将向我们展示查询是如何形成和执行的。我怀疑有连载inconsistency@NIck:我输入了post query.ToString结果。好的,现在您可以给我们展示一些数据库中EventProcessedutTime字段的示例吗?使用您建议的符号,我得到了相同的结果。返回零项能否检查最终查询的外观?我将其添加到Post中。此外,由于括号中的空字符串不被接受,指令deviceId.ToString生成异常。@SimoneSpagna抱歉,我意外添加了ToString内容。我测试了粘贴在这里的代码,效果很好。