gpt4 book ai didi

c# - Cosmos DB 和 Azure 函数 Get item .net 5 异步 foreach 语句无法对 FeedIterator 类型的变量进行操作

转载 作者:行者123 更新时间:2023-12-03 04:56:29 25 4
gpt4 key购买 nike

我正在尝试从 azure 函数 .net 5.0 中的 azure cosmos db 获取项目但我收到此错误,因为我使用异步方法

Feedlterator <rsakeys> Container.GetitemQuerylterator <rsakeys> (QueryDefinition queryDefinition, [string continuation Token = null], [QueryRequestOptions requestOptions = null]) (+ 1 overload) This method creates a query for items under a container in an Azure Cosmos database using a SQL statement with parameterized values.  It returns a Feedlterator.  For more information on preparing SQL statements with parameterized values, please see QueryDefinition.  
Returns:
An iterator to go through the items. CS8411: Asynchronous foreach statement cannot operate on variables of type 'Feediterator <rsakeys>' because 'Feedlterator <rsakeys>' does not contain a suitable public instance or extension definition for 'GetAsyncEnumerator
CS8411: Asynchronous foreach statement cannot operate on variables of type 'Feediterator <rsakeys>' because 'Feedlterator <rsakeys>' does not contain a suitable public instance or extension definition for 'GetAsyncEnumerator

enter image description here

这是我的代码,我根本没有找到任何关于此错误的好的文档,我几乎迷失了。

namespace projectA
{
class projectA
{
[Function("projectA")]
async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "")] HttpRequestData req,
FunctionContext context)
{

var logger = context.GetLogger("projectA");
logger.LogInformation("C# HTTP trigger function processed a request.");
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

/// The Azure Cosmos DB endpoint.
string EndpointUrl = "https://name.documents.azure.com:443/";

/// The primary key for the Azure DocumentDB account.
string PrimaryKey = "primarykey";

// The Cosmos client instance
CosmosClient cosmosClient = new CosmosClient(EndpointUrl, PrimaryKey);

Database database;
Container container;
string databaseId = "database1";
string containerId = "container1";

var sqlQueryText = "SELECT * FROM c WHERE c.key = '123'";
Console.WriteLine("Running query: {0}\n", sqlQueryText);
container = cosmosClient.GetContainer(databaseId, containerId);
QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);

List<rsakeys> rsakeyss = new List<rsakeys>();
//In this await foreach i get the error...
await foreach (rsakeys RsaKeys in container.GetItemQueryIterator<rsakeys>(queryDefinition))
{
rsakeyss.Add(RsaKeys);
Console.WriteLine("\tRead {0}\n", RsaKeys);
}
}
}
}

最佳答案

用于 Feed 迭代器的模式是

while (feedIterator.HasMoreResults)
foreach (var item in await feedIterator.ReadNextAsync())
Console.Write(item.Cost);

如果您想使用等待的 foreach (又名 IAsynEnumerable ),您可以推出自己的 IAsynEnumerable 产量

private static async IAsyncEnumerable<T> SomeMethod<T>(
Container container,
QueryDefinition queryDefinition,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
using var feedIterator = container.GetItemQueryIterator<T>(queryDefinition);
while (feedIterator.HasMoreResults)
foreach (var item in await feedIterator
.ReadNextAsync(cancellationToken)
.ConfigureAwait(false))
yield return item;
}

使用

await foreach(var item in SomeMethod<SomeType>(container, queryDefinition)
Console.Write(item);
<小时/>

额外事实

还值得注意的是ReadNextAsync返回Task<FeedResponse<T>>

public abstract Task<FeedResponse<T>> ReadNextAsync(CancellationToken cancellationToken = default);

FeedResponse继承自 Response

FeedResponse<T> : Response<IEnumerable<T>>, IEnumerable<T>

但是,为了方便起见Response有一个隐式运算符,因此可以隐式转换为其通用 Enumerbale 类型 IEnumerable<T>

public static implicit operator T(Response<T> response)

因此,以下两种说法均有效

using var feedIterator = container.GetItemQueryIterator<Bob>(queryDefinition);

FeedResponse<Bob> bobResponse = await feedIterator.ReadNextAsync()

IEnumerable<Bob> bobs = await feedIterator.ReadNextAsync()

根据评论更新

After doing using

var feedIterator = container.GetItemQueryIterator<rsakeys>(queryDefinition); var x =
await feedIterator.ReadNextAsync();
Console.WriteLine(x.GetType().ToString());

I get this console output :

Microsoft.Azure.Cosmos.QueryResponse`1[PolarisRSAFunction1NET5.rsakeys]

how do i access the query value?

调用ReadNextAsync将返回 FeedResponse<rsakeys>从那里您可以调用 x.Resource 返回IEnumerable<rsakeys>或者直接赋值

var response = await feedIterator.ReadNextAsync();
IEnumerable<rsakeys> items = response.Resource;

关于c# - Cosmos DB 和 Azure 函数 Get item .net 5 异步 foreach 语句无法对 FeedIterator 类型的变量进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68155231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com