Introduction to Language Integrated Query (LINQ)
Showing the Running Processes
In this example, you'll use LINQ to get the list of processes running on the local PC and display the list to the console.
using System;using System.Collections.Generic;using System.Linq;namespace LINQIntro{ class Program { static void Main(string[] args) { Program.ShowProcesses(); } public static void ShowProcesses() { // Select the list of processes and shape the output var processes = from p in System.Diagnostics.Process.GetProcesses() orderby p.ProcessName select new { p.ProcessName, p.Id, p.WorkingSet64, p.Threads, p.HandleCount }; // Display the selected records to the console foreach (var row in processes) { Console.WriteLine(row); } Console.ReadLine(); } }}
Accessing the Application Event Log
In this example, you'll use LINQ to read data from the Application Event Log. You'll search the EventLog for an error that contains a specific message. I chose the message to search for at random based on an error message I found in my local EventLog. You'll want to adjust the query statement to find something in your local EventLog. For anyone who has done anything with the EventLog and isn't yet a believer in LINQ, this example should give you a great example of the power and value of LINQ.
using System;using System.Collections.Generic;using System.Linq;namespace LINQIntro{ class Program { static void Main(string[] args) { Program.ShowEventLog(); } public static void ShowEventLog() { // Attach to the Application Event Log System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog(); myLog.Log = "Application"; // Query for an error record with "terminated" in the text // You'll want to adjust the EventLogEntryType and message // to find entries in your local event log // Notice how a custom object is being shaped var logEntries = from System.Diagnostics.EventLogEntry e in myLog.Entries where e.EntryType == System.Diagnostics.EventLogEntryType.Error && e.Message.Contains("terminated") select new { e.Source, e.InstanceId, e.Message, e.TimeGenerated }; // Display the selected records to the console foreach (var row in logEntries) { Console.WriteLine(row); } Console.ReadLine(); } }
Summary
You were given some introductory information about what LINQ is and LINQ is not. You've seen a brief background on the enabling language features. You've had an introduction to the LINQ syntax followed by a test drive of LINQ through a few examples. Hopefully, this gave you a good idea of the background behind LINQ and a better idea of the power that you can apply in your data access.
Future Columns
The topic of the next column is going to be either LINQ to XML or LINQ to SQL. If you have something else in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com.
About the Author
Mark Strawmyer is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in the architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fifth year in a row. You can reach Mark at mstrawmyer@crowechizek.com.
Page 3 of 3
This article was originally published on August 21, 2008