Introduction to Language Integrated Query (LINQ)
Test Driving LINQ Through Examples
Now that you've covered a little of the background, take LINQ for a test drive through a few handy examples. You'll look at how to use LINQ to read data from a structured type, a file, and an example that involves using LINQ to access the EventLog.
Accessing a Structured Type
You'll build on the example you were using earlier to show the syntax and query a structured type. You'll create a Customer structured type and type in some data from the Northwind database to fill your Customer structured type. You'll select all of the records where the City is not Berlin and then display them to the console. Because you're selecting Customer objects, the ToString method you'll add to your Customer will be used to display a comma-delimited list of attributes.
using System;using System.Collections.Generic;using System.Linq;namespace LINQIntro{ class Customer { public string CustomerName { get; set; } public string ContactName { get; set; } public string City { get; set; } public override string ToString() { return this.CustomerName + ", " + this.ContactName + ", " + this.City; } } class Program { static void Main(string[] args) { Program.ShowCustomers(); } public static void ShowCustomers() { // Build a list of customers using an object initializer List<Customer> customers = new List<Customer> { new Customer { CustomerName = "Alfreds Futterkiste", ContactName = "Maria Anders", City = "Berlin"}, new Customer { CustomerName = "Ana Trujillo Emparedados y helados", ContactName = "Ana Trujillo", City = "México D.F."}, new Customer { CustomerName = "Antonio Moreno Taquería", ContactName = "Antonio Moreno", City = "México D.F."}, new Customer { CustomerName = "Around the Horn", ContactName = "Thomas Hardy", City = "London"}, new Customer { CustomerName = "Berglunds snabbköp", ContactName = "Christina Berglund", City = "Luleå"}}; // Query the list of customers and select whatever // comes back var customer = from c in customers where c.City != "Berlin" orderby c.ContactName select c; // Display the selected records to the console foreach (var row in customer) { Console.WriteLine(row); } Console.ReadLine(); } }}
Showing a List of Files
In this example, you'll use LINQ to search a directory on the local PC for files that start with a particular name. Once again, I chose the directory and filename at random based on items on my computer. You'll want to adjust the directory and file in the query statement to ensure it will work for you.
using System;using System.Collections.Generic;using System.Linq;namespace LINQIntro{ class Program { static void Main(string[] args) { Program.ShowFiles(); } public static void ShowFiles() { // Point to a specific directory System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo( "C:\\Program Files\\Microsoft Visual Studio 9.0"); // Find files in the directory structure and select it var directoryList = from f in dirInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories) where f.Name.StartsWith("re") select f; // Display the selected records to the console foreach (var row in directoryList) { Console.WriteLine(row); } Console.ReadLine(); } }}
Page 2 of 3
This article was originally published on August 21, 2008