我对C#编程比较陌生,想要一些帮助。
来自DocumentDB的My Query返回一个类型为{System.Collections.Generic.List<object>}
的对象,其计数为1.有没有办法可以在零索引处选择元素而不迭代变量?
以下是查询字符串的外观
return _dbclient.CreateDocumentQuery<dynamic>(dblink,"select * from c " + sb.ToString()).ToList();
我没有使用实体FYI,数据是动态的,因此没有映射到对象
答案 0 :(得分:1)
1.你可以只是sql:SELECT top 1 * FROM c
2.您可以在sdk中使用FirstOrDefault()
。
private static readonly string endpointUrl = "https://***.documents.azure.com:443/";
private static readonly string authorizationKey = "***";
private static readonly string databaseId = "db";
private static readonly string collectionId = "item";
private static DocumentClient client;
public static async void QueryTest()
{
client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
Item queryItem = client.CreateDocumentQuery<Item>(uri , "select * from c")
.AsEnumerable()
.FirstOrDefault();
Console.WriteLine("\nRead {0}", queryItem);
}
}
3.您可以在FeedOptions参数中设置MaxItemCount = 1,因为@Sajeetharan说。
希望它对你有所帮助。
答案 1 :(得分:0)
您可以使用 FeedOptions
并将 MaxItemCount
设为1
return _dbclient.CreateDocumentQuery<dynamic>(dblink, "select * from c " + sb.ToString(), new FeedOptions { MaxItemCount = 1 });