<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Edwin M Sarmiento&gt;LINQ to Objects Sample from a systems administrator&#8217;s point-of-view &#8211; Edwin M Sarmiento</title>
	<atom:link href="https://www.edwinmsarmiento.com/linq-to-objects-sample-from-a-systems-administrators-point-of-view/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.edwinmsarmiento.com</link>
	<description>Intentional Excellence</description>
	<lastBuildDate>Mon, 13 Apr 2026 21:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">84283043</site>		<item>
		<title>&gt;LINQ to Objects Sample from a systems administrator&#8217;s point-of-view</title>
		<link>https://www.edwinmsarmiento.com/linq-to-objects-sample-from-a-systems-administrators-point-of-view/</link>
		<comments>https://www.edwinmsarmiento.com/linq-to-objects-sample-from-a-systems-administrators-point-of-view/#respond</comments>
		<pubDate>Tue, 22 Jan 2008 03:52:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2008/01/22/linq-to-objects-sample-from-a-systems-administrators-point-of-view</guid>

				<description><![CDATA[&#62;One way to test if the LINQ concepts work is to apply it to my daily task as an administrator. As documented, anytime you are working with a collection, like an array or a list, you can use LINQ. In this case, it&#8217;s LINQ to Objects. In this example, I&#8217;ll work with one of the [&#8230;]]]></description>
					<content:encoded><![CDATA[<p>&gt;<span style="font-family:arial;">One way to test if the LINQ concepts work is to apply it to my daily task as an administrator. As documented, anytime you are working with a collection, like an array or a list, you can use LINQ.  In this case, it&#8217;s LINQ to Objects. In this example, I&#8217;ll work with one of the most common task I do, and that is to check for running processes in servers.  I&#8217;m using the <a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.aspx">System.Diagnostics.Process</a> class to get all the active processes in my machine.  If you are doing a similar code in .NET 2.0, this is how it will look like</span><br /><span style="font-family:Arial;"></span><br /><strong><span style="font-size:85%;">System.Diagnostics.<span style="color:#009900;">Process</span>[] myProcesses;<br />myProcesses = System.Diagnostics.<span style="color:#009900;">Process</span>.GetProcesses();<br /><span style="color:#000099;">foreach</span> (System.Diagnostics.<span style="color:#009900;">Process</span> instance <span style="color:#000099;">in</span> myProcesses)<br />{<br /><span style="color:#009900;">//check for processes whose memory usage is grater than 10MB</span></span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">if</span> (instance.WorkingSet64 &gt; 10 * 1024 * 1024) </span></strong><br /><strong><span style="font-size:85%;">{<br /><span style="color:#009900;">Console</span>.WriteLine(<span style="color:#cc0000;"><span style="color:#990000;">&#8220;Name={0}, Mem Usage={1} bytes&#8221;</span>,</span> instance.ProcessName, instance.WorkingSet64);<br />}<br /><span style="color:#009900;">Console</span>.WriteLine(<span style="color:#990000;">&#8220;Press any key when your keyboard is attached . . . &#8220;</span>);<br />}<br /><span style="color:#009900;">Console</span>.ReadLine();</span></strong></p>
<p><span style="font-family:arial;">This code will extract all the processes in the local machine and displays those processes with memory usage greater than 10 MB. Now you might think that this is a bit easy with a few lines of codes.  What you&#8217;ll realize in the long run is, what if i need to sort them out in descending order and probably add other aggregate functions like grouping.  It would require that you include a sorting algorithm probably to sort out by memory usage and grouping for that matter.  What about counting the number of processes?  Then you would have that incremental counter that we&#8217;ve always used.  This is where LINQ to Objects make it quite a bit easy.  Let&#8217;s look at the codes for the same requirement, this time using LINQ.</span><br /><span style="font-family:Arial;"></span><br /><strong><span style="font-size:85%;"><span style="color:#000099;">var</span> procQuery = <span style="color:#000099;">from</span> p <span style="color:#000099;">in</span> System.Diagnostics.<span style="color:#009900;">Process</span>.GetProcesses()<br /><span style="color:#006600;">//check for processes whose memory usage is grater than 10MB</span><br /><span style="color:#000099;">where</span> p.WorkingSet64 &gt; 10 * 1024 * 1024<br /><span style="color:#000099;">select</span> p;<br /></span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">foreach</span> (<span style="color:#000099;">var</span> process <span style="color:#000099;">in</span> procQuery)<br /><span style="color:#009900;">Console</span>.WriteLine(<span style="color:#990000;">&#8220;Name = {0}, Mem Usage = {1} bytes&#8221;</span>,process.ProcessName, process.WorkingSet64);<br /><span style="color:#009900;">Console</span>.WriteLine(<span style="color:#990000;">&#8220;Press any key when your keyboard is attached . . . &#8220;</span>);<br /><span style="color:#009900;">Console</span>.ReadLine();</span></strong><br /><strong><span style="font-size:85%;"></span></strong><br /><span style="font-family:arial;">Notice that there isn&#8217;t much difference on the number of lines for both code sets.  Now, if we try includng sorting and grouping or all those aggregate functions, we just need to call a method if you are using LINQ instead of using a sorting algorithm or something else. An <strong><span style="font-size:85%;">orderby</span></strong> clause can be added to sort, a <strong><span style="font-size:85%;">Count()</span></strong> method can be used to count the number of instances instead of the all too common incremental counters, etc. Compare that without using LINQ (do I hear an &#8220;eow?&#8221;). </span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">I&#8217;ll probably extend this to something similar to Task Manager with all those fancy UI stuff and other fucntion but I&#8217;ll save that at a later post.  The original idea I had was to do something similar to a <a href="http://community.bartdesmet.net/blogs/bart/archive/2007/04/05/the-iqueryable-tales-linq-to-ldap-part-0.aspx">LINQ to LDAP</a> which happens to be a very long process (and I just wish Microsoft comes up with an API for this one as well in the long run for Active Directory-related stuff)</span></p>
<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/92377218009570869-2701073556653527652?l=bassplayerdoc.blogspot.com' alt='' /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/linq-to-objects-sample-from-a-systems-administrators-point-of-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">70</post-id>	</item>
	</channel>
</rss>