Linq – Do we need another way to access databases?

This was the question that was burning on my mind when I first heard about Linq.  I was skeptical and didn’t think it was something that we needed.  But after having started to use this new language, I have quickly become a believer.

Linq stands for language-integrated query and that is exactly what it is, it allows for rich metadata, compile-time syntax checking, static typing and IntelliSense on things that used to be imperative code.  This is not only a huge win from the error perspective, but also from the ease of use one.

For example, here is a sample query which includes populating an array to store the data:

using System;
using System.Linq;
using System.Collections.Generic;
 
class app {
  static void Main() {
    string[] names = { "Burke", "Connor", "Frank", 
                       "Everett", "Albert", "George", 
                       "Harris", "David" };
 
    IEnumerable<string> query = from s in names 
                               where s.Length == 5
                               orderby s
                               select s.ToUpper();
 
    foreach (string item in query)
      Console.WriteLine(item);
  }
}

You can see where we query the data, you just simply do something like “from s in names where s.Length == 5 orderby s select s.ToUpper();”  This clearly is close enough to SQL that anyone with experience with SQL programming will understand it and get used to doing this, but this means that if there is a typo, or any other problem in the query, you will find it at compile time.

Linq to SQL

For SQL, there are some specific things built into Linq.  You can read all about it here.  The main thing to know is that there is a DataContext object which does most of the interaction for you.  If you are in Visual Studio and create a Linq to SQL object in there,  you can point it to any/all of your tables and then access them in a very Object Oriented manner.  And when you are done changing/inserting things into the database, simply call DataContextInstance.SubmitChanges() and it will take care of updating things.

This is a very powerful and intuitive language and I am just starting to wrap my head around it.  Hope that this makes you want to try it out also and see what you think.

Published 20 October 08 06:00 by Tom
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# ASP NET Debugging Linq Do we need another way to access databases | Wood TV Stand said on May 31, 2009 7:16 PM:

PingBack from http://woodtvstand.info/story.php?id=12511

Leave a Comment

(required) 
(optional)
(required) 

Search

Go

This Blog

Syndication

Page view tracker