Naming is Hard

Icon

Written by Bruce Boughton

LINQ at VBUG

A bunch of us from Madgex launched a pincer movement on the Brighton Visual Basic User Group (VBUG) on Tuesday night to listen to a talk by Ian Cooper on LINQ.

LINQ (Language Integrated Query) is a .NET 3.0 technology which provides a type-safe query language integrated directly into the various CLR languages. It allows programmers to declaratively query data sets (in memory collections, databases, XML documents and entities) and is heavily based on SQL. For example, to query the Northwind customers database and select the names of all customers in Brighton in C# with LINQ to SQL you would write:

NorthwindDataContext db = new NorthwindDataContext();
var customers = from c in db.Customers
where c.City == "Brighton"
select c.Name;

The advantage of LINQ is that you have compile-time safety checking for your database queries. SQL is no longer embedded in strings. Also, because LINQ is a general query languages, you can query in-memory collections and XML documents in the same declarative fashion—hiding lines and lines of procedural boilerplate.

To be able to provide this type safety, LINQ to SQL requires a mapping between the database schema and the .NET code. Being a Microsoft technology, there are several different ways to achieve this. First of all, you can use the visual designer to drag and drop associations between database tables and business objects. Secondly, you can use attributes to mark up your pre-existing business objects or write database stubs. Thirdly, you can write an XML mapping file. Fourthly, you can run sqlmetal on the command line which creates database stubs based on your parameters. Of course, it is important to make sure that your mapping stays in sync with the database schema and the business object in your application and there will be different strategies for dealing with this.

Anyway, LINQ looks a very promising technology and Ian presented it with great enthusiasm and knowledge. Along the way we learnt about new .NET 3.0 constructs such as anonymous delegates, lamda expressions, and the var keyword, as well as the little-known .NET 2.0 yield keyword. Many thanks to VBUG for organising the event and for the drinks and pizza!

Category: .NET

Tagged:

One Response

  1. Andy Pattenden says:

    Sounds pretty cool