LINQ!

by Dave Tue 29 January 2008 @ 21:37

So as well as getting a more in-depth look at C#, I've also started looking at LINQ.  One word sums it up - wow! LINQ stands for "Language INtegrated Query" and it is basically a way of extending programming languages by providing them with classes to natively deal with queries.  So, instead of building a SqlConnection, SqlDataAdapter, SqlCommand you simply use a native command to get the data.  Its easier to show you than to describe, so I'll do just that.  The following is the original VB.Net code to pull out the links on the left of the page for previous entries.  It runs a stored procedure to get the data back and then pumps that out to a dataset. For brevity there are about fifty lines of code missing, but this is as small as I could cut it down...

   1: Dim sqlConn As ConnectionStringSettings
   2: sqlConn = ConfigurationManager.ConnectionStrings("SQLServer")
   3: Dim objConn As New SqlConnection(sqlConn.ConnectionString)
   4: objConn.Open()
   5: Dim objRecords As New SqlDataAdapter
   6: Dim objCommand As New SqlCommand
   7:  
   8: Dim objSetPrevLinks As New DataSet
   9: Dim objSetBlog As New DataSet
  10: Dim strMonth, strYear, strSQL As String
  11: Try
  12:     strSQL = "exec GetPrevPostCount"
  13:     objCommand = New SqlCommand(strSQL, objConn)
  14:     objRecords.SelectCommand = objCommand
  15:     objRecords.Fill(objSetPrevLinks)
  16:     objPrevEntries.DataSource = objSetPrevLinks
  17:     objPrevEntries.DataBind()
  18: Catch ex As Exception
  19:     BlogFucked.Text = ex.Message
  20: End Try
  21: objConn.Close()

This routine has worked for a couple of years now on the front page of DaveWhite.Net.  Next we'll look at the LINQ version. The first four lines are where the connection string gets setup, but the real work of executing the stored procedure is done on line 8. The stored procedure GetPrevPostCount has been converted to a native C# command.

   1: System.Configuration.Configuration rootWebConfig =
   2:     System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/csblog");
   3: System.Configuration.ConnectionStringSettings connString;
   4: connString = rootWebConfig.ConnectionStrings.ConnectionStrings["SQLServer"];
   5: try
   6: {
   7:     lpcDataContext lpcsql = new lpcDataContext(connString.ToString());
   8:     objPrevEntries.DataSource = lpcsql.GetPrevPostCount();
   9:     objPrevEntries.DataBind();
  10:     lpcsql.Connection.Close();
  11: }
  12: catch (Exception ex)
  13: {
  14:     FuckedBlog.Text = ex.Message;
  15: }

Now I'm sure there are even better ways to do this - after all this is my first day playing with LINQ.  Still, I have to admit I'm liking it so far.  I have no idea of the speed implications involved yet, but for this site that wouldn't make such a big difference anyway as the audience is so small. 

Categorised : Programming
Tagged with : ,


Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Johann Sebastian Bach's grave is here, along with the organ whose construction he advised on. Pretty awesome tbh :-)This is where US and Soviet forces met for the first time in world war two.Awesome awesome view from a restaurant on top of the Bastei!We had lunch in Meissen at the cafe in the famous porcelain factory, and ate from about €1000 worth of chinaA little light lunch and some wine...Dresden's famous frauenkirche, recently rebuilt after being destroyed in the second world war.