Access and Query Data Using the .NET Languages

Visual Studio 2008 introduced the language feature set called LINQ. LINQ is a programming model that takes advantage of many of the features discussed in this section. It provides language extensions that change the way you access and work with data. With it, you can work with your data using object syntax and query collections of objects using Visual Basic and C#.

You can use LINQ to map between data tables and objects. (See Chapter 13, “Working with Databases.”) In this way, you get an easier, more productive way to work with your data. This includes full IntelliSense support based on table and column names. It also includes support for managing inserts, updates, deletes, and reads.

The last of these, reading data, is a big part of LINQ in that it has built-in support for easily querying collections of data. Using LINQ features, you can query not only your data but also any collection in .NET. There are, of course, new keywords and syntax for doing so. Query operators that ship with Visual Basic, for example, include Select, From, Where, Join, Order By, Group By, Skip, Take, Aggregate, Let, and Distinct. The C# language has a similar set of keywords. And, if these are not enough, you can extend the built-in query operators, replace them, or write your own.

You use these query operators to query against any .NET data that implements the IEnumerable or IQueryable interface. This may include a DataTable, mapped SQL Server objects, .NET collections (including Generics), DataSets, and XML data.

Let’s look at an example. Suppose you had a collection of employee objects called employees and you wanted to access all the employees at a specific location. To do so, you might write the following function.

C#

public static IEnumerable<Employee> FilterEmployeesByLocation
  (IEnumerable<Employee> employees, string location)
{
  //LINQ query to return collection of employees filtered by location
  var emps = from Employee emp in employees
              where emp.Location.City == location
              select emp;

  return emps;
}

VB

Public Shared Function FilterEmployeesByLocation(
  ByVal employees As IEnumerable(Of Employee),
  ByVal location As String) As IEnumerable(Of Employee)

  'LINQ query to return collection of employees filtered by location
  Dim emps = From Employee In employees
             Where Employee.Location.City = location

  Return emps

End Function

Take a look at what is going on in the previous listing. The function takes a list of employee objects, filters it by a region passed to it, and then returns the results. Notice that to filter the list we create a LINQ in-memory query called emps. This query can be read like this: Looking at all the employee objects inside the employees collection, find those whose city matches the city passed into the function. Finally, we return emps as an IEnumerable<T> to allow the calling client to cycle through the results.

This is just a brief overview of LINQ. There are many things going on here, such as compile-time checking and schema validation (not to mention the LINQ language syntax). You will undoubtedly want to spend more time with LINQ.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset