c# - IReliableDictionary 上的 Linq 查询

标签 c# linq azure-service-fabric

我查看和修改了很多代码,但我还没有找到针对 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 查询的操作。我写了一些我想发布的,但他们首先需要一些润色。我有一种潜移默化的感觉,服务结构团队可能已经在处理它,但如果发生这种情况或何时发生,你的猜测和我的一样好。

关于c# - IReliableDictionary 上的 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141243/

相关文章:

c# - 为我所有的表单设置相同的图标

c# - 使用linq c#计算CSV文件中一列的平均值

Azure Service Fabric 集群预配问题

ssl - Azure Service Fabric 服务之间的 HTTPS

c# - 为持久数据创建和删除编写单元测试

c# - 如何使用 HttpClient 发出 OPTIONS 请求

c# - LINQ表达式无法翻译。以可以翻译的形式重写查询,或切换到客户端评估EF Core 3.1

c# - 从 Service Fabric 内部关闭 StatelessService 实例的最佳实践

c# - 从telnet服务器C#套接字读取响应

c# - LINQ - 从特定索引开始的列表中获取项目