Essential .NET 2.0
October 1st-5th
New York
October 15th-19th
Los Angeles

October 29th-2nd
Boston

November 5th-9th
Chicago


NEW- Essential Windows Communication Foundation
September 25th-28th
Los Angeles


NEW-Essential Windows Presentation Foundation
September 24th-28th
Los Angeles
October 15th-19th
Boston

October 22nd-26th
Bellevue, WA

Essential ASP.NET 2.0
September 24th-28th
Chicago
October 8th-12th
Los Angeles
October 22nd-26th
New York
November 5th-9th
Orlando


NEW- Essential Windows WorkFlow Foundation
October 22nd-26th
Los Angeles
November 5th-9th
Bellevue, WA

.NET Architecture &
Design 2.0
September 24th-28th
Boston
October 8th-12th
Orlando

October 22nd-25th
Chicago

November 5th-9th
New York


NEW-Essential SharePoint 2007 for Developers
October 1st-5th
Boston
October 15th-19th
Los Angeles
November 5th-9th
Denver

NEW-Essential AJAX for ASP.NET 2.0
November 6th-9th
Boston

Programming C#
November 5th-9th
Los Angeles

Essential SQL Server
October 22nd-26th
Boston











Introducing LINQ: Language-Integrated Query
By Anthony Sneed


Problem: Data != Objects

Developers live in two worlds: The world of objects and the world of data.  One of the things that make a developer's life more difficult is the impedance mismatch between these two worlds and the inordinate amount of plumbing code required to bridge the gap between them.  For example, we might want to retrieve customer data from a relational database, display it to the user for updating, then save those changes back to the database.  The traditional approach is to rely on an API like ADO.NET for the data-centric operations, potentially sacrificing on type-safety and compiler syntax checking, and utilize object-oriented programming techniques for other parts of our application.  Where these two worlds intersect in our application, we are forced to deal with differences between the SQL and CLR type systems, how entity relations work, as well as other incompatibilities.  Conversely, we may wish to deal with objects in a data-centric fashion, filtering, sorting and grouping collections of objects, but this means we may end up with code that is difficult to maintain because it obscures what we want to do at a higher level.

The Missing LINQ

Not wanting us to languish in this no man’s land, the good folks at Microsoft, led by Anders Hejlsberg (chief architect of the C# programming language), have created the LINQ Project.  LINQ stands for Language-Integrated Query and seeks to make query a first-class citizen of the programming language.  Microsoft plans to incorporate LINQ into C# 3.0 and Visual Basic 9.0, both of which will be released with Visual Studio 2008 (code-named Orcas).  By introducing a SQL-like syntax, LINQ allows you to interrogate an in-memory collection (anything implementing IEnumerable<T>), filter the results, group, sort, transform and perform a number of other data-centric operations.  Take, for example, the following array of strings:

string[] stooges = { "Moe", "Larry", "Curly", "Shemp" };

With C# 3.0’s new query syntax, you can extract only strings with a length of 5, sort them alphabetically, and change the results to upper case:

IEnumerable<string> query =
            from s in stooges
            where s.Length == 5
            orderby s
            select s.ToUpper();

In this snippet, from s in stooges indicates that you want to query the stooges collection and use s to represent each item in the sequence.  The select clause determines the data you want returned and what shape it should take.  Here we want to return each element converted to upper case.  The where and orderby clauses, as you might guess, restrict the results to those with 5 characters and specify an ascending sort.  Execution of the query is actually deferred until you iterate the query results, typically with a foreach statement, or by calling extension methods, such as ToArray or ToList, that perform the iteration internally (we’ll talk about extension methods in just a bit).  The following statement triggers the query execution:

foreach (string s in query) Console.WriteLine(s);

This produces the following output:

CURLY
LARRY
SHEMP

Behind the Magic:  C# Language Enhancements

Supporting language-integrated query is a set of powerful enhancements to the C# programming language, each of which is useful in its own right, but when taken together provide the basis for LINQ.  The most significant of these is extension methods, which allow third parties to, in essence, add methods to a type, thereby augmenting the type’s contract, while still allowing the type’s author to provide his or her own implementations of these methods.  Extension methods are nothing more than static methods defined in static classes, and (in C#) have the this operator in front of the first method parameter.  The System.Linq namespace contains what are called the standard query operators, which are extension methods that provide the basic functionality of LINQ.  The C# compiler, in fact, converts query syntax (things like from, in, where, select) to extension method calls.  One of these is the simplest query operator, Where:

namespace System.Linq
{
    public static class Enumerable
    {
        public static IEnumerable<T> Where<T>
            (this IEnumerable<T> source, Func<T, bool> predicate)
        {
            foreach (T item in source)
            {
                if (predicate(item)) yield return item;
            }
        }
    }
}

As you can see, the method accepts a source of type IEnumerable<T> and a predicate of the Func generic delegate type.  We can create an instance of the delegate as an anonymous method:

Func<string, bool> predicate = delegate(string s)
            { return s.Length == 5; };

The Where method simply iterates over the source, returning only items that pass the predicate test.  Because it’s defined as a static method, there’s nothing to prevent you from calling it directly, like so:

IEnumerable<string> query = Enumerable.Where(stooges, predicate);

However, because Where is defined as an extension method, we can invoke it using instance syntax (note, however, that it’s not really an instance method!):

IEnumerable<string> query = stooges.Where(predicate);

Extension methods are brought into scope when the namespace in which they are contained is imported with a using directive.  However, they are only used if no matching methods with the same name and signature exist on the target type or its base classes.  This lets you plug in your own query operators which take precedence over those in the System.Linq namespace.

Although you can define the predicate as an anonymous method (or a standalone method for that matter), C# provides a much more compact syntax, called a lambda expression.  If we were to re-write our anonymous method as a lambda expression and pass it as an argument, our call to Where would look like this (notice how the type of “s” is inferred and “return” is left out):

IEnumerable<string> query = stooges.Where(s => s.Length == 5);

The power of lambda expressions, however, is not just that they are compact (and hence more readable when passed as method parameters), but they can be compiled as either code or data.  When assigned to the generic type Expression<T>, the compiler emits an expression tree representing the code rather than IL that would execute the code.  Frameworks like LINQ to SQL can then analyze the expression tree to formulate SQL statements targeting a relational database.

Bridging the Gap: LINQ to SQL, Linq to XML

Besides enabling you to query in-memory collections, LINQ is based on an extensible architecture that allows you to plug in a query processing engine to fetch data from any external source.  In fact, the first version of LINQ comes with both these capabilities, called LINQ to SQL and LINQ to XML, respectively.  Combining these LINQ extensions, you could retrieve a list of products from the Northwind database, place the result in an in-memory collection, massage the data, then persist the output to an XML file, all without having to translate “foreign” data constructs into CLR objects, while at the same time benefiting from Intellisense and compile-time syntax checking.  Let’s start by querying relational data using LINQ to SQL:

NorthwindDataContext db = new NorthwindDataContext();

IEnumerable<Product> prodQuery =
            from p in db.Products
            where p.Category.CategoryName == "Beverages"
            select p;

The NorthwindDataContext class derives from DataContext, which does the heavy lifting to convert the query expression into SQL, execute it against the database, and return the results as a sequence of Product objects.  Where, might you ask, are these classes defined?  It just so happens that Visual Studio 2008 comes with special tool support for LINQ to SQL, including a class designer (which you get when adding a “LINQ to SQL Classes” item to a project).  The designer generates the NorthwindDataContext and Product classes when you drag the Products table from a data connection in the Server Explorer onto the class designer.  If you bring over multiple tables, relations between the tables are reflected in the class diagram and become object properties in the designer-generated code.

Notice how the where clause in our query references the CategoryName property of the Category class, which is shown to be a property of the Product class.  When you execute this query, LINQ to SQL generates a SQL statement that includes a JOIN on the Products and Categories tables.  The join is performed by SQL Server, which makes for efficient query execution.  You can see this take place by setting the Log property of the data context to dump output to the console window or a text file.

SELECT * FROM [dbo].[Products] AS [t0]
LEFT OUTER JOIN [dbo].[Categories] AS [t1]
ON [t1].[CategoryID] = [t0].[CategoryID]
WHERE [t1].[CategoryName] = @p0
-- @p0: Input String (Size = 9; Prec = 0; Scale = 0) [Beverages]

LINQ to SQL also includes support for tracking changes to in-memory objects, then posting those changes back to the database, while at the same time maintaining concurrency control.  (If you like, you can also specify stored procedures for the updates.)  For example, we can store our product list in a local collection, change the unit price of an item, then call SubmitChanges to persist the new data back to SQL Server.

List<Product> products = prodQuery.ToList();

Product chai = (from p in products
            where p.ProductName == "Chai"
            select p).Single();

chai.UnitPrice = 20;

db.SubmitChanges();

The ORM functionality of LINQ to SQL is somewhat limited (for example, many-to-many relationships aren’t fully supported and it only works with SQL Server).  But the power of LINQ really shines when combining LINQ to Objects, LINQ to SQL and LINQ to XML.  We can, for instance, take our list of beverages, sort them, and save them as an XML file.

IEnumerable<XElement> xmlQuery =
            from p in products
            orderby p.ProductName
            select new XElement("Product",
                        new XAttribute("ProductName", p.ProductName),
                        new XAttribute("UnitPrice", p.UnitPrice));

XElement bevXml = new XElement("Beverages", xmlQuery);

bevXml.Save("beverages.xml");

Notice how this code, unlike the DOM API, is element-centric, as opposed to document-centric, and that there’s no need for XPath query strings.  Nice.  The beverages.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Beverages>
  <Product ProductName="Chai" UnitPrice="20.0000" />
  <Product ProductName="Chang" UnitPrice="19.0000" />
  <Product ProductName="Chartreuse verte" UnitPrice="18.0000" />
  <Product ProductName="Côte de Blaye" UnitPrice="263.5000" />
  [Remaining items elided for clarity ...]
</Beverages>

Time to LINQ Up!

There is much more to LINQ than what I have room to cover here.  Other C# features driving LINQ are things like object initializers, anonymous types and implicitly typed local variables.  There are standard query operators that perform grouping, joins, partitioning, union, intersection, aggregation and conversion, and other functions as well.  And while LINQ does not completely resolve impedance mismatch between objects and data, it is a step in the right direction, resulting in cleaner code that more clearly reflects the developer’s intention.  It also makes you more productive by eliminating much of the plumbing code you would otherwise have to write, which, after all, is what frameworks are all about.

You can learn more about LINQ in our upcoming Guerrilla .NET Event November 12th-16th in Tampa, FL.

 

For more information email: onsites@develop.com or call 800.699.1932
 
Featured New Course

NEW-
Guerrilla Connected Systems
- Connecting WCF, Workflow & BizTalk 2006 R2

October 29th-November 2nd in Los Angeles register online or call 800.699.1932


What you’ll learn…
Using WCF, WF and BizTalk you explore how to integrate applications, explain why certain technologies are not suitable for particular types of applications, and demonstrate how your applications benefit from effective use of design patterns.

Highlights Include:

  • Application Design and Practices
  • Distributed Application Architecture and SOA
  • Design Patterns
  • Distributed Application Security
  • WCF Concepts and Architecture
  • Network and Queued Communication using WCF
  • WCF Security, CardSpace and Federation
  • Design by Contract, SOA and Service Factory
  • WF Concepts and Architecture
  • WF Activity model and hosting
  • Using WF for service composition
  • BizTalk Architecture and Orchestration
  • Scalability and Availability
  • Deployment and Application Monitoring


    Want to get a detailed course outline?

    CLICK HERE
    and put "Guerrilla Connected Systems" in the subject line.


 
NEW- .NET: MyTeam Custom Editions

We've developed 4 customizable .NET developer courses
based on the experience level of your team!

Start customizing your course today with a few simple answers. We'll recommend a custom edition that is designed for the experience level of your team. Then you can tailor the course to best support the application you're building.
Customize now: www.develop.com/myteam
 

Upcoming Guerrilla Events

What You Can Expect:

- Multiple instructors (2-5) per event

- Extended course hours
- A team of industry leading experts
- Leading-edge technologies
- Innovative course materials, including slides, labs, books, demos
- Your own PC and development platform
- Peer-to-peer collaboration and competition
- An exciting, multi-media learning environment


All Inclusive Price includes:
5-NIGHTS HOTEL, ALL MEALS AND BEVERAGES

ABOUT GUERRILLA EVENTS

To get a detailed course outline for any of the Guerrilla Events listed below, please send an email to requests@develop.com and put the course name in the subject line.

Featured Guerrilla Event

Guerrilla .NET
- Now includes: WCF, WPF, Workflow, LINQ and Silverlight
November 12th-16th in Tampa, FL register online or call 800.699.1932


What you’ll learn…
Guerrilla .NET gives you a comprehensive look at the modern .NET development. It will give you the skills and knowledge to build large reliable applications with confidence and troubleshoot them effectively.

This course also includes cutting-edge coverage of new .NET technologies. You’ll work with Windows Communication Foundation (WCF), build GUIs with Windows Presentation Foundation (WPF) and see how Windows Workflow Foundation can help you build workflow-based applications. You’ll also get to see upcoming technologies like Language Independent Query (LINQ) and Silverlight. You’ll get a chance to write some code and see how current technologies will evolve in the near future, helping you plan effectively.

Highlights Include:

  • Iterators and Anonymous Methods
  • Lambda Expression
  • Extension Methods
  • Implicit Typing and var
  • Dealing with Exceptions
  • Transactions
  • Threading and Concurrency
  • Memory Management
  • Using and Extending Visual Studio
  • Dealing with Production Errors using SOS and WinDBG
  • Windows Communication Foundation
  • Windows Presentation Foundation
  • Windows Workflow Foundation
  • Language Integrated Query (LINQ)
  • Silverlight


    Want to get a detailed course outline?
    CLICK HERE
    and put "Guerrilla .NET" in the subject line.

back to top

 

Upcoming Courses

To get a detailed course outline for any of the courses listed below, please send an email to requests@develop.com and put the course name in the subject line.

Essential .NET 2.0
October 1st-5th in New York register online or call 800.699.1932
October 15th-19th in Los Angeles register online or call 800.699.1932

October 29th-November 2nd in Boston register online or call 800.699.1932
November 5th-9th in Chicago register online or call 800.699.1932

What you’ll learn…
You will leave this course with a clear view of what .NET does (and does not) do, and how to start designing and building robust applications that take maximum advantage of the platform while avoiding common traps and pitfalls.

NEW- Essential Windows Communication Foundation
September 25th-28th in Los Angeles register online or call 800.699.1932

What you’ll learn…
Learn to build secure and reliable WCF applications. Upon completion you will know how to define and access services using channels, bindings, messages, data contracts, operation contracts, and faults and know the built-in functionality of WCF, including instance management, concurrency, reliability, queues, transactions and security.

NEW-Essential Windows Presentation Foundation
September 24th-28th in Los Angeles register online or call 800.699.1932
October 15th-19th in Boston register online or call 800.699.193
October 22nd-26th in Bellevue, WA register online
or call 800.699.1932


What you’ll learn…
Learn how to build rich Window and Web client applications using the new WPF API. Upon completion you will be able to write compelling user interfaces using WPF that include documents, media and user interaction and be able to take advantage of the new features of the platform including XAML, styling, templates, and commands.

Essential ASP.NET 2.0
September 24th-28th in Chicago register online or call 800.699.1932

October 8th-12th in Los Angeles register online or call 800.699.1932
October 22nd-26th in New York register online or call 800.699.1932
November 5th-9th in Orlando register online or call 800.699.1932


What you’ll learn…
Essential ASP.NET 2.0 is a week of in-depth exploration of the core issues involved with building browser-centric Web applications, where we will cover the latest technologies for architecting such applications. You will learn about the compilation engine of ASP.NET, see exactly how your .aspx pages are turned into .NET types, and learn how you can use those .NET types and others to build your Web sites.

NEW- Essential Windows WorkFlow Foundation
October 22nd-26th in Los Angeles register online or call 800.699.1932
November 5th-9th in Bellevue, WA register online or call 800.699.1932

What you’ll learn…
Learn how Windows WF technology fits with the rest of the Microsoft stack and learn when to best apply Windows WF technology to create the next generation applications.

.NET Architecture & Design 2.0
September 24th-28th in Boston register online or call 800.699.1932
October 8th-12th in Orlando register online or call 800.699.1932
October 22nd-26th in Chicago register online or call 800.699.1932
November 5th-9th in New York register online or call 800.699.1932


What you’ll learn…
Through discussion, demonstration and hands-on experience, this class provides essential and practical information for designers and senior developers building robust, reliable, high-performance distributed .NET applications.

NEW- Essential SharePoint 2007 for Developers
October 8th-12th in Boston register online or call 800.699.1932
October 15th-19th in Los Angeles register online or call 800.699.1932
November 5th-9th in Denver register online or call 800.699.1932


What you’ll learn…
For software developers and system architects that need to understand the architecture and programming techniques required to build collaborative applications using Windows SharePoint Services v3 and Microsoft Office SharePoint Server 2007

NEW-Essential AJAX for ASP.NET 2.0
November 6th-9th in Boston register online or call 800.699.1932

What you’ll learn…
If you are an experienced ASP.NET developer and you’re being asked to take your Web applications to the next level, this course is for you. Upon completion, you will know exactly when and where to use AJAX, how to integrate it into both new and old applications, and even how to extend it to give you the exact functionality you need for your application.

Programming C#
November 5th-9th in Los Angeles register online or call 800.699.1932

What you’ll learn…
In this course you will spend the majority of your time on the C# language. It moves quickly through the basics so you can cover the most important features in depth. C# 2.0 features such as generics, partial classes, static classes, global namespace qualification, etc. are integrated throughout the course.

Essential SQL Server for Developers
October 22nd-26th in Boston register online or call 800.699.1932

What you’ll learn…
This course is designed for experienced SQL Server developers who want to take advantage of the latest features and functions in the new SQL Server 2005 release. Highlights include how and when to use Service Broker, Notification Services, and native Web services. Outside of the database, the client side has been greatly enhanced with support for new data types, functional enhancements in SqlClient, client-side XQuery functionality, additions to the .NET XML stack.

back to top

 

Community Blogs

Visual Studio Tricks Series: #3 Managing the Recent Projects List
Sunday, September 09, 2007
Michael Kennedy .NET and Agile Software Design

Organizing your using statements
Thursday, September 06, 2007
Mark Smith writes whatever nonsense is on his mind...

Certificate based Authentication and WCF (Mode independent)
Sunday, August 26, 2007
Dominick Baier on .NET, security and other stuff

To read all our instructor blogs, go to: www.develop.com/blogs

back to top