My first Linq steps

First of all, I would like to wish you all a happy new year. I know it’s a bit late for that, but I didn’t get (or take…) a chance to post my first blogpost of 2008 earlier. I’m eager to find out what this next year is going to bring us, but I guess I’ll have to wait another 11 1/2 months to be sure 😉

Now, to start off the new year with some tech-talk, I wanted to write a little bit about Linq. Linq is the new way of talking to your database, as far as Microsoft is concerned. It’s kind-off a replacement of the DataSet with DataTables and TableAdapters used in .NET 2.0 projects. Replacement might not be the best way of putting it, because as far as I know, Linq doesn’t really store data the way a DataSet does (keeping DataSets still a very usefull way of storing relational data).

So what does Linq do then? Well, basically it’s a layer between your code and the actual database which enables you to access objects in an object relational manner without you having to deal with T-SQL statements. This means you don’t have to worry about creating JOINs, Views, Insert, Update and Delete queries. Linq will take care of that, providing you a consistent way of manipulating data in multiple data storage systems. That’s right: Linq doesn’t work with SQL Server only! There are a few different kinds of Linq out there, including SQL, XML, Objects, DataSets (hey they’re still usefull!) and even Google has it’s own Linq.

To get an idea of the power of Linq, you can take a look at the videos at which give a pretty good impression of what you can do (or better: leave out) with Linq. For those of you not wanting to listen to all of the videos, here’s what I did in my test project.

– Created a database with some tables and relations; all set up the way you’re supposed to design a relational database
– Created a new web project in Visual Studio 2008 (you need this for Framework 3.5 related things)
– Added a Linq document to my project (in this case Linq to SQL classes). Offcourse you could and would place it in a separate project normally, your Data Access Layer.
– Used the Server Explorer to connect to my database and drag ‘n drop some tables onto the design surface of the Linq file.

Untill this point all the steps are equal to the steps you would take creating a new project with an ADO.NET DataSet layer, but now you would have to specify a query to fetch data, one for updating, one for deleting, etc. Notice I’m not going to do all  this 🙂

– Create a new ASPX page and drop a grid onto it.
– Configure the datasource of the grid to be a new LinqDataSource and select (after compiling first…) the newly created Linq object which maps to the table you want to display
– Run the project!

Now, the neat thing is that Linq is taking care of constructing the correct SQL statements. Even if we turn on things like sorting and paging in our grid; nothing has to change. Linq just alters the SQL query to include the correct sorting, or limits the resultset to return only the results on the page you selected.

Offcourse, the same goes for inserting, updating and deleting items in the database. When bound to a formview for instance, all this is taken care of by Linq and you have no worries whatsoever.

Cool, right? I’m going to play with it a bit more before really putting it to use (we’re not using VS2008 at the office yet), but I’m already a big fan to say the least. This is definitely going to be a huge time saver as far as I’m concerned.

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *