[英]How to convert an IGroupedObservable to IGrouping?
I have an observable sequence of elements that have a char Key
property, that has values in the range from 'A'
to 'E'
.我有一个可观察的元素序列,它们具有
char Key
属性,其值在'A'
到'E'
的范围内。 I want to group these elements based on this key.我想根据这个键对这些元素进行分组。 After grouping them I want the result to by an observable of groups, so that I can process each group separately.
在对它们进行分组后,我希望通过一组可观察的组得到结果,以便我可以分别处理每个组。 My problem is that I can't find a nice way to preserve the key of each group in the final observable.
我的问题是我找不到一个很好的方法来保存最终观察到的每个组的密钥。 Here is an example of what I am trying to do:
这是我正在尝试做的一个例子:
var observable = Observable
.Interval(TimeSpan.FromMilliseconds(100))
.Take(42)
.GroupBy(n => (char)(65 + n % 5))
.Select(grouped => grouped.ToArray())
.Merge();
observable.Subscribe(group =>
Console.WriteLine($"Group: {String.Join(", ", group)}"));
Output: Output:
Group: 0, 5, 10, 15, 20, 25, 30, 35, 40
Group: 1, 6, 11, 16, 21, 26, 31, 36, 41
Group: 2, 7, 12, 17, 22, 27, 32, 37
Group: 3, 8, 13, 18, 23, 28, 33, 38
Group: 4, 9, 14, 19, 24, 29, 34, 39
The groups are correct, but the keys ( 'A'
- 'E'
) are lost.组是正确的,但键(
'A'
- 'E'
)丢失了。 The type of the observable
is IObservable<long[]>
. observable
的类型是IObservable<long[]>
。 What I would like it to be instead, is an IObservable<IGrouping<char, long>>
.相反,我希望它是一个
IObservable<IGrouping<char, long>>
。 This way the group.Key
would be available inside the final subscription code.这样
group.Key
将在最终订阅代码中可用。 But as far as I can see there is no built-in way to convert an IGroupedObservable
(the result of the GroupBy
operator) to an IGrouping
.但据我所知,没有内置方法可以将
IGroupedObservable
( GroupBy
运算符的结果)转换为IGrouping
。 I can see the operators ToArray
, ToList
, ToLookup
, ToDictionary
etc, but not a ToGrouping
operator.我可以看到运算符
ToArray
、 ToList
、 ToLookup
、 ToDictionary
等,但看不到ToGrouping
运算符。 My question is, how can I implement this operator?我的问题是,我该如何实现这个运算符?
Here is my incomplete attempt to implement it:这是我实现它的不完整尝试:
public static IObservable<IGrouping<TKey, TSource>> ToGrouping<TKey, TSource>(
this IGroupedObservable<TKey, TSource> source)
{
return Observable.Create<IGrouping<TKey, TSource>>(observer =>
{
// What to do?
return source.Subscribe();
});
}
My intention is to use it in the original example instead of the ToArray
, like this:我的意图是在原始示例中使用它而不是
ToArray
,如下所示:
.Select(grouped => grouped.ToGrouping())
This does most of what you want:这可以满足您的大部分需求:
var observable = Observable
.Interval(TimeSpan.FromMilliseconds(100))
.Take(42)
.GroupBy(n => (char)(65 + n % 5))
.SelectMany(grouped => grouped.ToArray().Select(a => (key: grouped.Key, results: a)));
That's an IObservable<ValueTuple<TKey, TResult[]>
.那是一个
IObservable<ValueTuple<TKey, TResult[]>
。 If you wanted the IGrouping
interface, you would have to make an object, since I don't think there's one available for you:如果您想要
IGrouping
接口,则必须制作一个 object,因为我认为您没有可用的接口:
public static class Grouping
{
// Because I'm too lazy to code types
public static Grouping<TKey, TResult> Create<TKey, TResult>(TKey key, IEnumerable<TResult> results)
{
return new Grouping<TKey, TResult>(key, results);
}
}
public class Grouping<TKey, TResult> : IGrouping<TKey, TResult>
{
public Grouping(TKey key, IEnumerable<TResult> results)
{
this.Key = key;
this.Results = results;
}
public TKey Key { get; }
public IEnumerable<TResult> Results { get; }
public IEnumerator<TResult> GetEnumerator()
{
return Results.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Results.GetEnumerator();
}
}
then your observable becomes:那么你的 observable 变成:
var o2 = Observable.Interval(TimeSpan.FromMilliseconds(100))
.Take(42)
.GroupBy(n => (char)(65 + n % 5))
.SelectMany(grouped => grouped.ToArray().Select(a => Grouping.Create(grouped.Key, a)));
This seems to be what you want:这似乎是你想要的:
IObservable<(char Key, long[] Values)> observable =
Observable
.Interval(TimeSpan.FromMilliseconds(100))
.Take(42)
.GroupBy(n => (char)(65 + n % 5))
.Select(grouped => new { Key = grouped.Key, Values = grouped.ToArray() })
.SelectMany(x => x.Values, (k, v) => (Key: k.Key, Values: v));
observable.Subscribe(group =>
Console.WriteLine($"Group {group.Key}: {String.Join(", ", group.Values)}"));
I get:我得到:
Group A: 0, 5, 10, 15, 20, 25, 30, 35, 40
Group B: 1, 6, 11, 16, 21, 26, 31, 36, 41
Group C: 2, 7, 12, 17, 22, 27, 32, 37
Group D: 3, 8, 13, 18, 23, 28, 33, 38
Group E: 4, 9, 14, 19, 24, 29, 34, 39
I found a way to implement the ToGrouping
operator without creating a custom class that implements the IGrouping
interface.我找到了一种实现
ToGrouping
运算符的方法,而无需创建实现IGrouping
接口的自定义 class。 It is more succinct but less efficient than Shlomo's solution .它比 Shlomo 的解决方案更简洁但效率更低。
/// <summary>
/// Creates an observable sequence containing a single 'IGrouping' that has the same
/// key with the source 'IGroupedObservable', and contains all of its elements.
/// </summary>
public static IObservable<IGrouping<TKey, TSource>> ToGrouping<TKey, TSource>(
this IGroupedObservable<TKey, TSource> source)
{
return source
.ToList()
.Select(list => list.GroupBy(_ => source.Key).Single());
}
This implementation assumes that the TKey
type does not implement the IEquatable
interface in some crazy way, that returns different hashcodes for the same value, or considers a value not equal to itself.此实现假定
TKey
类型没有以某种疯狂的方式实现IEquatable
接口,即为相同的值返回不同的哈希码,或者认为值不等于自身。 In case that happens, the Single
LINQ operator will throw an exception.如果发生这种情况,
Single
LINQ 运算符将抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.