How Do I: LINQ to SQL: Overview

New language features for C# and VB support data access directly from code, without writing SQL. This video shows the minimum steps to use LINQ (Language Integrated Query) to query, update, add and delete rows from the Northwind database.

Presented by Scott Stanfield

Duration: 9 minutes, 23 seconds

Date: 15 November 2007

Watch VB video  or  C# video   |   Download VB video  or  C# video   |   Get VB code  or  C# code

Video downloads: WMVWMV | ZuneZune | iPodiPod | PSPPSP | MPEG-4MPEG-4 | 3GP3GP

Comments : 11

Leave a Comment

mcp111 : On March 04, 2008 5:32 PM said:

This is a great overview but I'm not really clear if LINQ is going to simplify my programming. Seems to be more complicated than SQL.

I would also like to comment on the download links presentation. You have them as "WMV WMV", etc - initailly I thought it was some kind of typo but when I hovered over them I found that one was for C# and the other was for VB. You need to present things in a more "user-friendly" manner with neat sub-headings as VB downloads and C# downloads so it is clear and neat!!!

pfdpereira : On March 05, 2008 7:10 AM said:

just one question! where can i get the password for video zip files included in this page available to download?

thanks,

  Paulo Pereira

mcp111 : On March 05, 2008 11:54 AM said:

When you post the comments on this website it would be more correct to say "On March 04, 2008 mcp111 said ... "

You have the order all mixed up and it doesn't read well.

mcp111 : On March 05, 2008 3:22 PM said:

Actually I'm not sure why you have the audio downloads section. For a demo video is a must!

Freon22 : On March 19, 2008 4:08 PM said:

I agree somewhat with mcp111 as far as simple queries, updates, or inserts. But I see Linq being a great help on complex queries, updates, and inserts.

Not sure yet if using Linq will be any faster at run time. Has anyone tested to see if the runtime is the same as with writing your own t-sql statement.

Anway good job so far on the videos.

igavemybest : On March 29, 2008 1:19 AM said:

I actually just upgraded to VS2008 just for LINQ because I am doing a project that has extremely complicated SQL staments that are based off of variables that change depending on other changing variables, and on and on.  I alway thought it would be so easy to do in VB.  Well, now I can, freakin awesome!  And by the way... runtime seems to be just as quick becuase all the SQL codebehind is actually there

Freon22 : On April 02, 2008 11:15 AM said:

I do have one problem with Linq! It does not seem to have a easy way to handle null returns.

Like a user types in his login name and password but he made a mistake while typing his password. Now when you run the linq query it will not return anything and will throw an error. I have tryed the DefaultIfEmpty and a few other default types. Now I maybe coding it all wrong? Don't really know because there is not much infor, available on the net yet. Maybe someday someone will post some examples on how to work around these simple but painful errors.

So if you want to use linq with a gridview, dropdown list and so on then Linq is great. But if all you want to do is to check a few values in a database or check to see if they are even in the database. Then save yourself the headache and use a t-sql query.

Freon22 : On April 02, 2008 10:55 PM said:

Yesterday was not a good day, it seemed that I could not do a simple query that may have a null return. A few kind coders showed me where I was messing up. The funny thing about this is at one time I had it right. But for some unknown reason I failed to see it working when I tested it. So I moved on to trying to do it wrong again. lol I guess after seeing errors on every try I missed it when it worked.

So I want to show a simple example on how to handle a null return or I should say how I needed to handle it in my code. In t-sql we could always use the reader.HasRows but in Linq you need to use DefaultIfEmpty, SingleOrDefault, or one of the other Defaults.

registerDataContext db = new registerDataContext();

       var users = (from user in db.userAccounts

                    where user.email == TextBox1.Text

                    select user).SingleOrDefault();

       //Check if users is null.

       if (users != null)

       {

           Label1.Text = string.Format("<h2>The email address <font color='red'>{0}</font> already exist!</h2>", TextBox1.Text);

       }

       else

       {

           Label1.Text = string.Format("<h2>The email address <font color='red'>{0}</font> does not exist!</h2>", TextBox1.Text);

       }

Another way of making this same query is.

       registerDataContext db = new registerDataContext();

       userAccount users = db.userAccounts.SingleOrDefault(p => p.email == TextBox1.Text);

       if (users != null)

       {

           Label1.Text = string.Format("<h2>The email address <font color='red'>{0}</font> is already exist!</h2>", TextBox1.Text);

       }

       else

       {

           Label1.Text = string.Format("<h2>The email address <font color='red'>{0}</font> does not exist!</h2>", TextBox1.Text);

       }

I hope this may save someone else from pulling out their hair.

TheDirtyBird : On April 23, 2008 4:13 PM said:

Good intro video.  

suyog.dabhole : On May 15, 2008 2:36 AM said:

very helpful series.

fher : On May 15, 2008 10:23 PM said:

For some reason the sample code in the Default.aspx file first line reads in part like this:

CodeBehind="Default.aspx.vb" Inherits="LINQPart1._Default"

But the sample code also includes a “Default.aspx.vb” file which handles the vb code, in order for the samples to work and be editable change that to something like:

CodeFile="Default.aspx.vb" Inherits="_Default"

That is if you want to do the very same thing Scott is doing and follow along

Leave a Comment

You must be logged in to leave a comment. Click here to log in.

Featured ASP.NET 3.5 Web Hosting


Featured ASP.NET 3.5 hosting offer from AppliedI.net

Click Here To Learn More...