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
@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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With