Export (0) Print
Expand All

Observable.MaxBy<TSource, TKey> Method (IObservable<TSource>, Func<TSource, TKey>)

Returns the elements in an observable sequence with the maximum key value.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)

public static IObservable<IList<TSource>> MaxBy<TSource, TKey>(
	this IObservable<TSource> source,
	Func<TSource, TKey> keySelector
)

Type Parameters

TSource

The type of source.

TKey

The type of key.

Parameters

source
Type: System.IObservable<TSource>
An observable sequence to get the maximum elements for.
keySelector
Type: System.Func<TSource, TKey>
The key selector function.

Return Value

Type: System.IObservable<IList<TSource>>
The elements in an observable sequence with the maximum key value.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IObservable<TSource>. When you use instance method syntax to call this method, omit the first parameter. For more information, see http://msdn.microsoft.com/en-us/library/bb384936.aspx or http://msdn.microsoft.com/en-us/library/bb383977.aspx.

The MaxBy operator is used obtain the items in the sequence that generate a maximum key value. For example, if the sequence was a sequence of all processes running on a machine the MaxBy operator could be used to return the processes that have allocated the most physical memory . The example code in this topic demonstrates this.

The following example code creates an observable sequence of all running processes on the local machine. Then the MaxBy operator is used to return an observable sequence which contains a list of the processes with that have allocated the most physical memory. The process information for the processes in the list is written to the console window.

using System;
using System.Reactive.Linq;
using System.Diagnostics;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************//
      //*** Generate a sequence of processes running on the local machine.                        ***//
      //*********************************************************************************************//

      var seqProcesses = System.Diagnostics.Process.GetProcesses().ToObservable();


      //*********************************************************************************************//
      //*** Use the MaxBy operator to get a list of the processes that have the highest amount    ***//
      //*** of physical memory allocated.                                                         ***//
      //*********************************************************************************************//

      var maxWorkingSet = seqProcesses.MaxBy(p => p.WorkingSet64);


      //*********************************************************************************************//
      //*** Write the process information to the console window for the processes which have      ***//
      //*** allocated the most physical memory                                                    ***//
      //*********************************************************************************************//

      maxWorkingSet.Subscribe(maxList => 
      {
        foreach (Process process in maxList)
        {
          Console.WriteLine("\nDescription   : {0}\n" + 
                            "Filename      : {1}\n" + 
                            "Working Set   : {2}", 
                            process.MainModule.FileVersionInfo.FileDescription, 
                            process.MainModule.FileName, 
                            process.WorkingSet64);
        }
      });

      Console.WriteLine("\nPress ENTER to exit...\n");
      Console.ReadLine();
    }
  }
}

The following output was generated by the example code.


Description   : Desktop Window Manager
Filename      : C:\Windows\system32\Dwm.exe
Working Set   : 363646976

Press ENTER to exit...

Show:
© 2014 Microsoft