Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to objects - select first object

I know almost nothing about linq.

I'm doing this:

var apps = from app in Process.GetProcesses()
    where app.ProcessName.Contains( "MyAppName" ) && app.MainWindowHandle != IntPtr.Zero
    select app;

Which gets me all the running processes which match that criteria.

But I don't know how to get the first one. The examples I can find on the net seem to imply I have to do this

var matchedApp = (from app in Process.GetProcesses()
    where app.ProcessName.Contains( "MyAppName" ) && app.MainWindowHandle != IntPtr.Zero
    select app).First();

which strikes me as somewhat ugly, and also throws an exception if there are no matching processes. Is there a better way?

UPDATE

I'm actually trying to find the first matching item, and call SetForegroundWindow on it

I've come up with this solution, which also strikes me as ugly and awful, but better than above. Any ideas?

var unused = from app in Process.GetProcesses()
    where app.ProcessName.Contains( "MyAppName" ) && app.MainWindowHandle != IntPtr.Zero
    select SetForegroundWindow( app.MainWindowHandle ); // side-effects in linq-query is technically bad I guess
like image 336
Orion Edwards Avatar asked Aug 11 '08 03:08

Orion Edwards


2 Answers

@FryHard FirstOrDefault will work but remember that it returns null if none are found. This code isn't tested but should be close to what you want:

var app = Process.GetProcesses().FirstOrDefault(p => p.ProcessName.Contains("MyAppName") && p.MainWindowHandle != IntPtr.Zero);

if (app == null)
    return;

SetForegroundWindow(app.MainWindowHandle);
like image 85
Matt Hamilton Avatar answered Sep 23 '22 01:09

Matt Hamilton


Do not use Count() like ICR says. Count() will iterate through the IEnumerable to figure out how many items it has. In this case the performance penalty may be negligible since there aren't many processes, but it's a bad habit to get into. Only use Count() when your query is only interested in the number of results. Count is almost never a good idea.

There are several problems with FryHard's answer. First, because of delayed execution, you will end up executing the LINQ query twice, once to get the number of results, and once to get the FirstOrDefault. Second, there is no reason whatsoever to use FirstOrDefault after checking the count. Since it can return null, you should never use it without checking for null. Either do apps.First().MainWindowHandle or:

var app = apps.FirstOrDefault();

if (app != null)
    SetForegroundWindow(app.MainWindowHandle);

This is why the best solution is Mark's, without question. It's the most efficient and stable way of using LINQ to get what you want.

like image 42
David Schwartz Avatar answered Sep 23 '22 01:09

David Schwartz