>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’s LINQ to Objects. In this example, I’ll work with one of the most common task I do, and that is to check for running processes in servers. I’m using the System.Diagnostics.Process 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
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process instance in myProcesses)
{
//check for processes whose memory usage is grater than 10MB
if (instance.WorkingSet64 > 10 * 1024 * 1024)
{
Console.WriteLine(“Name={0}, Mem Usage={1} bytes”, instance.ProcessName, instance.WorkingSet64);
}
Console.WriteLine(“Press any key when your keyboard is attached . . . “);
}
Console.ReadLine();
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’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’ve always used. This is where LINQ to Objects make it quite a bit easy. Let’s look at the codes for the same requirement, this time using LINQ.
var procQuery = from p in System.Diagnostics.Process.GetProcesses()
//check for processes whose memory usage is grater than 10MB
where p.WorkingSet64 > 10 * 1024 * 1024
select p;
foreach (var process in procQuery)
Console.WriteLine(“Name = {0}, Mem Usage = {1} bytes”,process.ProcessName, process.WorkingSet64);
Console.WriteLine(“Press any key when your keyboard is attached . . . “);
Console.ReadLine();
Notice that there isn’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 orderby clause can be added to sort, a Count() 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 “eow?”).
I’ll probably extend this to something similar to Task Manager with all those fancy UI stuff and other fucntion but I’ll save that at a later post. The original idea I had was to do something similar to a LINQ to LDAP 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)
Please note: I reserve the right to delete comments that are offensive or off-topic.