Feeds:
Posts
Comments

Archive for the ‘.NET’ Category

Introduction to LINQ

The Webcast of my Microsoft Techdays 2008 session, "An Introduction to LINQ", is available (in french):

Part 1
Part 2
Part 3

On the same site you will find the Webcasts for over 300 sessions (in french) of the Microsoft Techdays 2008, including sessions on Visual Studio 2008, C# 3.0, Silverlight, SQL Server 2008, F# and more…

Read Full Post »

Tomorrow, I'll be giving a talk at the Microsoft TechDays 2008 conference. This is the main developer-related Microsoft event here in France. There will be about 280 technical sessions and I’ve heard the organizers expect 15,000 attendees! The subject of my talk is LINQ, a set of new technologies added by Microsoft to languages such as C# and Visual Basic. LINQ aims at radically simplifying the way object-oriented languages deal with objects and other information models such as relational databases or XML infosets.

I've been hooked on LINQ since the beginning, when its ideas started to be experimented in the context of the Cω research project. My buddy Fabrice Marguerie, with whom I'll be giving the talk, describes LINQ in his new book, LINQ in Action:

LINQ stands for Language Integrated Query. In a nutshell, it makes query operations like SQL statements into first-class citizens in .NET languages like C# and VB. LINQ offers built-in support for querying in-memory object collections such as arrays or lists, XML, DataSets, and relational databases. But LINQ is extensible and can be used to query various data sources […]

In addition to offering novel approaches to deal with data, LINQ represents a shift toward declarative and functional programming. When people ask me for reasons to learn LINQ, I tell them that they should learn it in order to be able to use it with XML, relational data, or in-memory collections, but above all to be able to start using declarative programming, deferred execution, and lambda expressions.

While LINQ and OOPAL (the array programming model adopted by F-Script) are based on very different principles, they share a common goal: providing a higher-level programming model that allows for more expressive and powerful language constructs. We can illustrate this with the following example, comparing some typical code in a classic object-oriented language (here, Java) with the code in C# with LINQ and F-Script with OOPAL.
In this example we deal with the object model of an airplane company:

Say we have a collection of flight objects named flights. Starting there, we want to get the names of the pilots in charge of a flight to Paris on a B747 airplane. And we want them sorted by salary in increasing order.

Java


TreeSet<Pilot> pilots = new TreeSet<Pilot>(new Comparator()
{
    public int compare(Object o1, Object o2)
    {
        if (((Pilot)o1).salary() < ((Pilot)o2).salary())
            return -1;
        else if (((Pilot)o1).salary() == ((Pilot)o2).salary())
            return 0;
        else
            return 1;
    }
});

for (flight : flights)
{
    if (flight.arrivalLocation().equals("PARIS") && flight.airplane().model.equals("B747"))
    {
        pilots.add(flight.pilot());
    }
}

ArrayList<String> result = new ArrayList<String>();

for (pilot : pilots)
{
    result.add(pilot.name());
} 

C# (with LINQ)


var pilots = (from flight in flights 
              where flight.arrivalLocation == "Paris" && flight.airplane.model == "B747" 
              select flight.pilot).Distinct();

var result = from pilot in pilots 
             orderby pilot.salary
             select pilot.name;

F-Script (with OOPAL)


pilots := (flights at:flights arrivalLocation = 'PARIS' & (flights airplane model = 'B747')) pilot distinct.

result := pilots name at:pilots salary sort.

The three programs manipulate plain old standard objects (respectively Java, .NET and Cocoa objects). However, the programming models on which they are based differ and, as you can see, the C# and F-Script versions are shorter and more expressive.

If you are around and want to know more about LINQ, you can come to my session!

Read Full Post »