gpt4 book ai didi

c# - IReliableDictionary 上的 Linq 查询

转载 作者:行者123 更新时间:2023-11-30 15:21:37 24 4
gpt4 key购买 nike

我查看和修改了很多代码,但我还没有找到针对 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/

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