<p>I know almost nothing about linq.</p>

<p>I'm doing this:</p>

<pre><code>var apps = from app in Process.GetProcesses()
    where app.ProcessName.Contains( "MyAppName" ) &amp;&amp; app.MainWindowHandle != IntPtr.Zero
    select app;
</code></pre>

<p>Which gets me all the running processes which match that criteria.</p>

<p>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</p>

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

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

<p><strong>UPDATE</strong></p>

<p>I'm actually trying to find the first matching item, and call <code>SetForegroundWindow</code> on it</p>

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

<pre><code>var unused = from app in Process.GetProcesses()
    where app.ProcessName.Contains( "MyAppName" ) &amp;&amp; app.MainWindowHandle != IntPtr.Zero
    select SetForegroundWindow( app.MainWindowHandle ); // side-effects in linq-query is technically bad I guess
</code></pre>

[{'answer_id': 7509, 'author': 'FryHard', 'author_id': 231, 'author_profile': 'https://Stackoverflow.com/users/231', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>var apps = from app in Process.GetProcesses()\nwhere app.ProcessName.Contains( "MyAppName" ) &amp;&amp; app.MainWindowHandle != IntPtr.Zero\nselect app;\n\nif (apps.Count &gt; 0)\n{\n    SetForegroundWindow(apps.FirstOrDefault().MainWindowHandle );\n}\n</code></pre>\n'}, {'answer_id': 7512, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 5, 'selected': True, 'text': '<p>@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:</p>\n\n<pre><code>var app = Process.GetProcesses().FirstOrDefault(p =&gt; p.ProcessName.Contains("MyAppName") &amp;&amp; p.MainWindowHandle != IntPtr.Zero);\n\nif (app == null)\n    return;\n\nSetForegroundWindow(app.MainWindowHandle);\n</code></pre>\n'}, {'answer_id': 8190166, 'author': 'David Schwartz', 'author_id': 399124, 'author_profile': 'https://Stackoverflow.com/users/399124', 'pm_score': 2, 'selected': False, 'text': '<p>Do <em>not</em> use <code>Count()</code> like ICR says. <code>Count()</code> will iterate through the <code>IEnumerable</code> 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 <code>Count()</code> when your query is only interested in the <em>number of results.</em> <code>Count</code> is almost never a good idea.</p>\n\n<p>There are several problems with FryHard\'s answer. First, because of <a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx" rel="nofollow" title="Derik Whittaker on LINQ&#39;s Delayed Execution">delayed execution</a>, you will end up executing the LINQ query twice, once to get the number of results, and once to get the <code>FirstOrDefault</code>. Second, there is no reason whatsoever to use <code>FirstOrDefault</code> after checking the count. Since it can return null, you should never use it without checking for null. Either do <code>apps.First().MainWindowHandle</code> or:</p>\n\n<pre><code>var app = apps.FirstOrDefault();\n\nif (app != null)\n    SetForegroundWindow(app.MainWindowHandle);\n</code></pre>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/7503', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/234/']