Now that LINQ is available to all developers with the release of Visual Studio 2008, it is going to take some time to realize all of its uses. I now find I examine a .NET class library object and see if any properties implement an IEnumerable collection interface.
Like the EventLog component does. You drag this component onto a form and you immediately can read and write to and from a Windows Event Log. I immediately wanted to see if I could use LINQ to filter and query the entries.
My final aim is to make an RSS publisher for certain event log entries that match a filter. But for this example, I just wanted to display and filter the event log sources into a grid.

The full code can be Downloaded here or a full step-by-step available on the HookedONLINQ Wiki site here.
The two important LINQ query bits look like this:
private void MainForm_Load(object sender, EventArgs e)
{
// retrieve a list of all event log sources, and populate the drop-down combo box
eventLog.Log = "Application";
var sources = (from EventLogEntry es in eventLog.Entries
orderby es.Source
select es.Source).Distinct();
comboBoxSource.DataSource = sources.ToList();
} private void comboBoxSource_SelectedIndexChanged(object sender, EventArgs e)
{
// retrieve all the event log entries matching the source selection in the combo-box
var query = from EventLogEntry el in eventLog.Entries
where el.Source == comboBoxSource.Text
orderby el.TimeGenerated descending
select new
{
Time = el.TimeGenerated,
Source = el.Source,
Message = el.Message
};
dataGridView.DataSource = query.ToList();
}You can see that is pretty simple queries. To do the same using loops and if statements would have been considerable more work though. Keep an eye out for any type that exposes a list property, and keep LINQ in mind when using that property. If you are seeing lots of complicated for loops and if statements - you should refactor and use a LINQ query instead.
Troy.