How can I pass this to a lambda expression?

2

I want to do this in a code that only takes one line, who can help me would be great.

long processSize = 0;
Process bProcess = null;
foreach (var process in Process.GetProcessesByName(name).Where(x => x.PagedMemorySize64 > processSize))
{
    processSize = process.PagedMemorySize64;
    bProcess = process;
}

FindProcess.Invoke(null, new ProcessEventArgs(bProcess));
    
asked by Mr.Noone 08.08.2016 в 23:43
source

1 answer

1

There are several things to mark

  • GetProcessesByName () method already returns an array you do not need to use the ToArray ()
  • you are running the GetProcessesByName () twice which is little performente

I would not advise doing it on a line because the code is horrible, unstable and unclear, but if you could do something like this:

Process[] processList = Process.GetProcessesByName(name);

Process p = processList.OrderByDescending(process => process.PagedMemorySize64).First();

FindProcess.Invoke(null, new ProcessEventArgs(p));

separating the code into several lines is not wrong

    
answered by 09.08.2016 / 04:07
source