Question

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
Was it helpful?

Solution

@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);

OTHER TIPS

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.

Assuming that in your first example apps is an IEnumerable you could make use of the .Count and .FirstOrDefault properties to get the single item that you want to pass to SetForegroundWindow.

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

if (apps.Count > 0)
{
    SetForegroundWindow(apps.FirstOrDefault().MainWindowHandle );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top