返回具有最大键值的可观测序列中的元素。
              Namespace:System.Reactive.Linq
              装配: System.Reactive.dll) 中的 System.Reactive (
语法
'Declaration
<ExtensionAttribute> _
Public Shared Function MaxBy(Of TSource, TKey) ( _
    source As IObservable(Of TSource), _
    keySelector As Func(Of TSource, TKey) _
) As IObservable(Of IList(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim keySelector As Func(Of TSource, TKey)
Dim returnValue As IObservable(Of IList(Of TSource))
returnValue = source.MaxBy(keySelector)
public static IObservable<IList<TSource>> MaxBy<TSource, TKey>(
    this IObservable<TSource> source,
    Func<TSource, TKey> keySelector
)
[ExtensionAttribute]
public:
generic<typename TSource, typename TKey>
static IObservable<IList<TSource>^>^ MaxBy(
    IObservable<TSource>^ source, 
    Func<TSource, TKey>^ keySelector
)
static member MaxBy : 
        source:IObservable<'TSource> * 
        keySelector:Func<'TSource, 'TKey> -> IObservable<IList<'TSource>> 
JScript does not support generic types and methods.
类型参数
- TSource
源的类型。 
-               TKey
键的类型。 
参数
- source
类型: System.IObservable<TSource>
要获取其最大元素的可观察序列。 
- keySelector
类型: System.Func<TSource、TKey>
键选择器函数。 
返回值
类型: System.IObservable<IList<TSource>>
具有最大键值的可观测序列中的元素。
使用说明
在 Visual Basic 和 C# 中,可以将此方法作为 IObservable<TSource> 类型的任何对象的实例方法调用。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅或。
备注
MaxBy 运算符用于获取序列中生成最大键值的项。 例如,如果序列是计算机上运行的所有进程的序列,则 MaxBy 运算符可用于返回已分配最多物理内存 的进程。 本主题中的示例代码对此进行了演示。
示例
以下示例代码在本地计算机上创建所有正在运行的进程的可观察序列。 然后,MaxBy 运算符用于返回可观测序列,该序列包含已分配最多物理内存的进程列表。 列表中进程的进程信息将写入控制台窗口。
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();
    }
  }
}
示例代码生成了以下输出。
Description   : Desktop Window Manager
Filename      : C:\Windows\system32\Dwm.exe
Working Set   : 363646976
Press ENTER to exit...