编程语言
首页 > 编程语言> > C#-IReliableDictionary上的Linq查询

C#-IReliableDictionary上的Linq查询

作者:互联网

我已经做了很多查找和弄乱代码的工作,但还没有找到对IReliableDictionary进行Linq查询的方法.我知道它与标准IDictionary不同,但是我很好奇是否有人有幸.我开始认为不幸的是这是不可能的.

解决方法:

当前,没有正常方法对可靠的字典进行linq查询.话虽如此,您可以做一些事情.如前所述,CreateEnumerableAsync方法存在的原因是因为服务结构将可靠的字典分页到磁盘,但是如果您知道基础集合很小并且对性能没有影响,那么以下类将起作用.

public static class AsyncEnumerableExtensions
{
    /// <summary>
    /// Converts the collection to a list
    /// </summary>
    /// <typeparam name="TValType">value type of the collection</typeparam>
    /// <param name="enumerator">enumerator to convert</param>
    /// <param name="ct">cancellation token for the async operations</param>
    /// <param name="tx">tx to enforce that this is called in a transactional context</param>
    /// <returns>a list containing all elements in the origin collection</returns>
    public static async Task<IList<TValType>> ToListAsync<TValType>(
        this IAsyncEnumerator<TValType> enumerator,CancellationToken ct, ITransaction tx)
    {
        IList<TValType> ret = new List<TValType>();
        while (await enumerator.MoveNextAsync(ct).ConfigureAwait(false))
        {
            ret.Add(enumerator.Current);
        }
        return ret;
    }

    /// <summary>
    /// Converts the collection to a list
    /// </summary>
    /// <typeparam name="TValType">value type of the collection</typeparam>
    /// <param name="enumerator">enumerator to convert</param>
    /// <param name="tx">tx to enforce that this is called in a transactional context</param>
    /// <returns>a list containing all elements in the origin collection</returns>
    public static Task<IList<TValType>> ToListAsync<TValType>(
        this IAsyncEnumerator<TValType> enumerator, ITransaction tx)
    {
        return enumerator.ToListAsync(CancellationToken.None,tx);
    }

    /// <summary>
    /// Converts the collection to a list
    /// </summary>
    /// <typeparam name="TValType">value type of the collection</typeparam>
    /// <param name="enumerable">enumerator to convert</param>
    /// <param name="ct">cancellation token for the async operations</param>
    /// <param name="tx">tx to enforce that this is called in a transactional context</param>
    /// <returns>a list containing all elements in the origin collection</returns>
    public static Task<IList<TValType>> ToListAsync<TValType>(this IAsyncEnumerable<TValType> enumerable,
        CancellationToken ct, ITransaction tx)
    {
        return enumerable.GetAsyncEnumerator().ToListAsync(ct,tx);
    }

    /// <summary>
    /// Converts the collection to a list
    /// </summary>
    /// <typeparam name="TValType">value type of the collection</typeparam>
    /// <param name="enumerable">enumerator to convert</param>
    /// <param name="tx">tx to enforce that this is called in a transactional context</param>
    /// <returns>a list containing all elements in the origin collection</returns>
    public static Task<IList<TValType>> ToListAsync<TValType>(this IAsyncEnumerable<TValType> enumerable, ITransaction tx)
    {
        return enumerable.GetAsyncEnumerator().ToListAsync(tx);
    }
}

您还可以实现自己的自定义枚举器和扩展方法,以对数据执行linq查询之类的高效操作.我写了一些我想发布的内容,但他们首先需要润色.我有一个偷偷摸摸的感觉,服务结构团队可能已经在其中,但是如果发生或何时发生,您的猜测与我的一样好.

标签:azure-service-fabric,linq,c
来源: https://codeday.me/bug/20191027/1941444.html