LINQ-May-2008

Page history last edited by Diane Wilson 1 yr ago

 LINQ - Language INtegrated Query for .NET 3.5


First meeting, Monday, May 12, 2008, 6:00 pm at Compuware in Durham


Welcome to LINQ!

 

This will be our first meeting, and I hope that everyone who has an interest in LINQ will be able to attend. In this first meeting, I'd like to learn what each of you expects to gain from an in-depth study of LINQ. We'll use that to shape and direct our schedule and topics.

 

Slides and sample code 

 

So what is LINQ? LINQ is a query capability that is built-in to C# 3.0 and VB.NET 9.0, the language versions included in Visual Studio 2008, and also into the .NET 3.5 framework. When I say that it is "built-in", that means that it is incorporated into the syntax of the languages, and makes use of the .NET 3.5 framework to define and execute queries, and to process the results. Queries and results are strongly typed objects.

 

By definition, LINQ can only query objects that implement IEnumerable<T>. This includes many collection types within .NET, and there is an AsEnumerable() method that will convert other collections such as ArrayLists and DataTables. LINQ includes a completely new object model for XML, and also includes the IQueryable<T> interface that allows implementation of data providers for any external data source, such as a relational database. .NET 3.5 includes a provider for SQL Server in the framework. Additional providers for other databases, web and WCF services, and new components such as the Entity Framework will certainly be coming soon.

 

LINQ is defined with two syntax models. One is a SQL-like query structure that looks like this:

 

  • var myQuery1 = from cat in NW.Categories where cat.CategoryID == 2 select cat;

 

The same query can be expressed as method calls:

 

  • var myQuery1 = NW.Categories.where(cat => cat.CategoryID == 2);

 

Both of these queries are syntactically correct C# statements, but they don't look anything like C# from previous versions. Given a DataContext class for the Northwind database (shown above as "NW"), both of these statements will generate (but not execute) valid SQL queries to retrieve data from the Northwind database.

 

To understate the obvious, a lot has changed. The place to start is with the new language features in C# and VB, so that the LINQ queries above will start to make sense. These features include

 

  • Implicit typing
  • Anonymous types
  • Object initialization
  • LINQ syntax
  • Lambda expressions
  • Typing of LINQ queries and result sets
  • Extension methods
  • Partial methods

 

Once we've gotten through the language basics, we'll be ready to start on the basic LINQ operators, viewing LINQ queries and results in the Visual Studio debugger, detailing the differences between implicit and explicit typing for LINQ queries, and understanding the mechanics of deferred and immediate query execution.

 

That should be more than enough to take us through the first meeting!

 

See ya there,

Diane

Comments (0)

You don't have permission to comment on this page.