对可观测序列的元素进行分组,并使用指定的函数选择生成的元素。
Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (
语法
'Declaration
<ExtensionAttribute> _
Public Shared Function GroupBy(Of TSource, TKey, TElement) ( _
source As IObservable(Of TSource), _
keySelector As Func(Of TSource, TKey), _
elementSelector As Func(Of TSource, TElement) _
) As IObservable(Of IGroupedObservable(Of TKey, TElement))
'Usage
Dim source As IObservable(Of TSource)
Dim keySelector As Func(Of TSource, TKey)
Dim elementSelector As Func(Of TSource, TElement)
Dim returnValue As IObservable(Of IGroupedObservable(Of TKey, TElement))
returnValue = source.GroupBy(keySelector, _
elementSelector)
public static IObservable<IGroupedObservable<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
this IObservable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector
)
[ExtensionAttribute]
public:
generic<typename TSource, typename TKey, typename TElement>
static IObservable<IGroupedObservable<TKey, TElement>^>^ GroupBy(
IObservable<TSource>^ source,
Func<TSource, TKey>^ keySelector,
Func<TSource, TElement>^ elementSelector
)
static member GroupBy :
source:IObservable<'TSource> *
keySelector:Func<'TSource, 'TKey> *
elementSelector:Func<'TSource, 'TElement> -> IObservable<IGroupedObservable<'TKey, 'TElement>>
JScript does not support generic types and methods.
类型参数
- TSource
类型源。
- TKey
类型键。
- TElement
类型元素。
参数
- source
类型: System.IObservable<TSource>
要对其元素进行分组的可观察序列。
- keySelector
类型: System.Func<TSource、TKey>
用于提取每个元素的键的函数。
- elementSelector
类型: System.Func<TSource、TElement>
用于将每个源元素映射到可观测组中的元素的函数。
返回值
类型: System.IObservable<IGroupedObservable<TKey、TElement>>
一系列可观测组,其中每个组对应一个唯一键值,其中包含共享同一键值的所有元素。
使用说明
在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅或。
备注
GroupBy 运算符用于根据键值将项的源序列分组到组中。 每个键值都是 keySelector 函数的结果,可以派生自源序列中的每个项。 GroupBy 运算符的结果是由 IGroupedObservable<TKey, TElement> 序列表示的分组项序列。 IGroupedObservable 公开用于标识分组序列的键属性。 生成的分组序列中的实际项由应用于每个源项的 elementSelector 函数的结果控制。
示例
此示例代码生成过去六小时内发生的事件日志记录序列。 然后,GroupBy 运算符与 LINQ 语句一起使用,根据计算结果为“Error”或“Critical”的 LevelDisplayName 属性对错误事件记录进行分组。 如果为 true,则键选择器函数的结果为 true,导致将事件记录分组到错误组中。 GroupBy 运算符的结果是 IGroupedObservable<布尔值 EventRecord>。 此 IGroupedObservable 序列订阅以访问分组错误事件日志条目。 代码检查组序列,以查找键值为 true 的组。 true 的键值标识错误组。 然后订阅该错误组,并将错误事件日志条目写入控制台窗口。
using System;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Diagnostics.Eventing.Reader;
using System.IO;
namespace Example
{
class Program
{
static void Main()
{
//************************************************************************************************//
//*** Generate a sequence of event log entries that have been written to the system event log ***//
//*** within the last 6 hours. ***//
//************************************************************************************************//
const int eventLogTimeSpan = 6;
EventLogReader sysEventLog = new EventLogReader("System");
//****************************************************************************************************//
//*** Start with the last entry in the event log. ***//
//*** Stop on an event generated at a time more than the EventLogTimeSpan (6 hrs in this example). ***//
//*** Each iteration function will step back one entry. ***//
//*** The resultSelector function will just select each entry for inclusion in the sequence. ***//
//*** The ThreadPool schedule schedules the processing of the event log on a thread pool thread ***//
//****************************************************************************************************//
sysEventLog.Seek(SeekOrigin.End,0);
EventRecord lastEntry = sysEventLog.ReadEvent();
var eventLogEntrySeq = Observable.Generate(lastEntry,
x => (DateTime.Now - x.TimeCreated) < TimeSpan.FromHours(eventLogTimeSpan),
x => {sysEventLog.Seek(x.Bookmark,-1); return sysEventLog.ReadEvent();},
x => x,
Scheduler.ThreadPool);
//************************************************************************************************************//
//*** Use the GroupBy operator with LINQ to group the sequence into entries that are errors (key=true) and ***//
//*** those that are not errors (key=false). ***//
//************************************************************************************************************//
var eventLogGroupedSeq =
from entry in eventLogEntrySeq
group entry by (entry.LevelDisplayName == "Error") || (entry.LevelDisplayName == "Critical") into groupedEntries
select groupedEntries;
//***************************************************************************************//
//*** eventLogGroupedSeq is a IGroupedObservable<Boolean, EventRecord>. Subscribing ***//
//*** will return a sequence of the groups. For this example, we only want the group ***//
//*** where the key is true indicating the Error entries group. So we then subscribe ***//
//*** to that grouped sequence to write the error entries that occurred within the ***//
//*** last 6 hours. ***//
//***************************************************************************************//
eventLogGroupedSeq.Subscribe(groupedSeq =>
{
if (groupedSeq.Key == true)
{
groupedSeq.Subscribe(evtEntry => Console.WriteLine("ID : {0}\n" +
"Type : {1}\n" +
"Source: {2}\n" +
"Time Generated: {3}\n" +
"Message: {4}\n",
evtEntry.Id,
evtEntry.LevelDisplayName,
evtEntry.ProviderName,
evtEntry.TimeCreated.ToString(),
evtEntry.FormatDescription()));
}
});
Console.WriteLine("\nDisplaying error entries from the system event log\n" +
"that occurred within the last {0} hours...\n\n" +
"Press ENTER to exit...\n",eventLogTimeSpan);
Console.ReadLine();
}
}
}
以下输出是使用示例代码生成的。
Displaying error entries from the system event log
that occurred within the last 6 hours...
Press ENTER to exit...
ID : 34001
Type : Error
Source: Microsoft-Windows-SharedAccess_NAT
Time Generated: 5/31/2011 4:39:18 AM
Message: The ICS_IPV6 failed to configure IPv6 stack.
ID : 34001
Type : Error
Source: Microsoft-Windows-SharedAccess_NAT
Time Generated: 5/31/2011 4:01:36 AM
Message: The ICS_IPV6 failed to configure IPv6 stack.
ID : 34001
Type : Error
Source: Microsoft-Windows-SharedAccess_NAT
Time Generated: 5/31/2011 3:49:29 AM
Message: The ICS_IPV6 failed to configure IPv6 stack.
ID : 34001
Type : Error
Source: Microsoft-Windows-SharedAccess_NAT
Time Generated: 5/31/2011 3:11:47 AM
Message: The ICS_IPV6 failed to configure IPv6 stack.
ID : 34001
Type : Error
Source: Microsoft-Windows-SharedAccess_NAT
Time Generated: 5/31/2011 2:59:40 AM
Message: The ICS_IPV6 failed to configure IPv6 stack.