linq 如何将IEEE802.11方法转换为异步工作[重复]

rkttyhzu  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(107)

此问题在此处已有答案

async Task<IEnumerable> throws "is not an iterator interface type" error(2个答案)
Asynchronous iterator Task<IEnumerable>(3个答案)
5天前关闭。
我用的是C#,.net 6.0
我正在尝试转换IEEE802.11方法和ot的调用者方法以异步工作。
我有一个代码,看起来像这样:

public IEnumerable<MyUser> getUsers()
    {
        return AccountById
            .Keys
            .SelectMany(accountId => {
                try
                {
                    return getUsersFor(accountId)
                        .Select(x => new MyUser(x,
                            SetAccountUniqueName(AccountById[accountId].Name,
                                AccountById[accountId].Id)));
                }
                catch (Exception ex)
                {
                    _log.Error(ex);
                    return Enumerable.Empty<MyUser>();
                }
            });
    }

    public IEnumerable<User> getUsersFor(string accountId)
    {
        ListUsersResponse usersResponse;
        string marker = null;
        do
        {
            using (var myClient = new ServiceClient(pass))
            {
                usersResponse =
                    myClient.ListUsersAsync(
                            new ListUsersRequest { Marker = marker, MaxItems = MAX_ITEMS })
                        .Result;
                foreach (var user in usersResponse.Users)
                {
                    yield return user;
                }
            }

            marker = usersResponse.Marker;
        } while (usersResponse.IsTruncated);
    }

字符串
如何将getUsers()和getUsersFor()转换为JavaScript方法?

nwo49xxi

nwo49xxi1#

除非我漏了什么你只需要把签名从

public IEnumerable<User> getUsersFor(string accountId)

字符串

public async Task<IEnumerable<User>> getUsersFor(string accountId)


并在你的aprc方法中删除对.Result的调用并等待它们:

usersResponse = await myClient
    .ListUsersAsync(new ListUsersRequest { Marker = marker, MaxItems = MAX_ITEMS });

ljsrvy3e

ljsrvy3e2#

您需要删除.Result并将方法更改为与asyncawait关键字异步,并将返回类型更改为Task<IEnumerable<MyUser>>Task<IEnumerable<User>>

public async Task<IEnumerable<MyUser>> GetUsersAsync()
{
    var result = new List<MyUser>();

    foreach(var accountId in AccountById.Keys)
    {
        try
        {
            var user = await GetUsersForAsync(accountId);
            result.Add(new MyUser(user, SetAccountUniqueName(AccountById[accountId].Name,
                            AccountById[accountId].Id)));
        }
        catch (Exception ex)
        {
                _log.Error(ex);
                return Enumerable.Empty<MyUser>();
        }
    }
}

public async Task<IEnumerable<User>> GetUsersForAsync(string accountId)
{
    ListUsersResponse usersResponse;
    string marker = null;
    do
    {
        using (var myClient = new ServiceClient(pass))
        {
            usersResponse =
                await myClient.ListUsersAsync(
                        new ListUsersRequest { Marker = marker, MaxItems = MAX_ITEMS });

            foreach (var user in usersResponse.Users)
            {
                yield return user;
            }
        }

        marker = usersResponse.Marker;
    } while (usersResponse.IsTruncated);
}

字符串

相关问题