Aracılığıyla paylaş


Observable.GroupBy<TSource, TKey, TElement> Yöntemi (IObservable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>)

Gözlemlenebilir bir dizinin öğelerini gruplar ve belirtilen bir işlevi kullanarak sonuçta elde edilen öğeleri seçer.

Ad Alanı:System.Reactive.Linq
Derleme: System.Reactive (System.Reactive.dll içinde)

Syntax

'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.

Tür Parametreleri

  • Tsource
    Tür kaynağı.
  • Tkey
    Tür anahtarı.
  • Telement
    Tür öğesi.

Parametreler

  • kaynak
    Tür: System.IObservable<TSource>
    Öğeleri gruplandırılan gözlemlenebilir bir dizi.
  • Keyselector
    Tür: System.Func<TSource, TKey>
    Her öğenin anahtarını ayıklamak için bir işlev.
  • Elementselector
    Tür: System.Func<TSource, TElement>
    Her kaynak öğeyi gözlemlenebilir bir gruptaki bir öğeye eşleyen işlev.

Dönüş Değeri

Tür: System.IObservable<IGroupedObservable<TKey, TElement>>
Her biri benzersiz bir anahtar değerine karşılık gelen ve aynı anahtar değerini paylaşan tüm öğeleri içeren gözlemlenebilir gruplar dizisi.

Kullanım Notu

Visual Basic ve C# içinde bu yöntemi IObservable<TSource> türündeki herhangi bir nesnede örnek yöntemi olarak çağırabilirsiniz. Bu yöntemi çağırmak için örnek yöntemi sözdizimini kullandığınızda, ilk parametreyi yok sayın. Daha fazla bilgi için bkz. veya .

Açıklamalar

GroupBy işleci, bir öğe kaynak dizisini bir anahtar değerine göre gruplar halinde gruplandırmak için kullanılır. Her anahtar değeri keySelector işlevinin sonucudur ve kaynak dizideki her öğeden türetilebilir. GroupBy işlecinin sonucu , IGroupedObservable<TKey, TElement dizisiyle temsil edilen gruplandırılmış öğeler dizisidir>. IGroupedObservable, gruplandırılmış diziyi tanımlayan bir anahtar özelliği sunar. Sonuçta elde edilen gruplandırılmış dizilerdeki gerçek öğeler, her kaynak öğeye uygulanan elementSelector işlevinin sonucuyla denetlenir.

Örnekler

Bu örnek kod, son altı saat içinde gerçekleşen olay günlüğü kayıtlarının sırasını oluşturur. Ardından GroupBy işleci, "Error" veya "Critical" olarak değerlendirilen LevelDisplayName özelliğini temel alan hata olan olay kayıtlarını gruplandırmak için BIR LINQ deyimiyle birlikte kullanılır. Bu doğruysa, anahtar seçici işlevinin sonucu true olur ve sonuç olarak olay kaydı hata grubunda gruplandırılır. GroupBy işlecinin sonucu bir IGroupedObservable<Boole değeri olan EventRecord'tur>. Gruplandırılmış hata olay günlüğü girişlerine erişmek için bu IGroupedObservable dizisi abone olur. Kod, anahtar değerinin true olduğu grubu bulmak için grup dizisini inceler. true anahtar değeri hata grubunu tanımlar. Ardından bu hata grubuna abone olur ve hata olay günlüğü girişleri konsol penceresine yazılır.

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();
    }
  }
}

Aşağıdaki çıkış örnek kodla oluşturulmuştur.

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.

Ayrıca Bkz.

Başvuru

Gözlemlenebilir Sınıf

GroupBy Aşırı Yüklemesi

System.Reactive.Linq Ad Alanı