Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

LINQ to SQL came out with 3.5 and I am still amazed that many people accomplished to stay away from that simple technology. When you ask them, they've heard about but they are not sure how everything works together. Instead of looking it up to learn to see if there is something that may improve their development, they just ignore it.

If you work in software, its your job to keep yourself on the loop for new technologies and languages coming out. Don't need to download everything from Codeplex and play with it, but you should include in your technical book reading a blog/news reading. Of course I keep broadcasting to everybody that hasn't turn me off by now, to subscribe to Community Blogs in ASP.NET and of course ScottGu's blog.

I've included in my interviews to ask a simple question, if you are applying for a ASP.NET job. Do you know who is Scoot Guthrie? You'll be amazed how many "ASP.NET developers" don't even know the name. Shocking!

Wanted to show you how to select something from a database and insert a row. For the exercise will use a simple table with a latitude, longitude and ID. If you have used previously dataAdapters aka DataSets in ASP.NET 2.0 you'll find LINQ to SQL familiar. Create a new Item and select LINQ to SQL, drag and drop all the tables you want.

Note: If you modify the table, the LINQ to SQL won't refresh, you'll have to delete and drag and drop again.

image

Select statement you'll find that LINQ provides you with from, where and select out of the box, so you can interrogate the table.

image

Select statement returning strong type. You'll find the => new in 3.5 for LINQ.

image

Insert a new row, make sure you got a primary key in each table you are trying to update. The syntax is familiar if you have been using DataSets/DataTable/DataRow

image

This is as simple as it goes to Select and Insert into a table using LINQ. I would recommend to run this very simple exercise for yourself. Grab an existing database, create a ASP.NET app and generate the LINQ to SQL for the database. Then try to select and insert using this simple example.

We learned by doing things and practicing. After that you'll need to figure it how to update in LINQ to SQL. I bet that after you do that, you'll start writing all your projects with a Database layer using LINQ to SQL.

Cheers

Al

Published Wednesday, April 23, 2008 11:25 PM by albertpascual

Comments

# re: Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

Thursday, April 24, 2008 8:10 AM by ca8msm

"I bet that after you do that, you'll start writing all your projects with a Database layer using LINQ to SQL."

Personally, I don't like the idea of using LINQ to SQL in my data layer. If I make a simple change in my database logic (for example, I now want to exclude records with a certain status that has just been added as an extra column in the database) I would have to:

1. Refresh the LINQ objects

2. modify my LINQ query to exclude the records with this status

3. Recompile and redeploy my application.

I think I'll be sticking to a stored procedure approach where I simply make one change in the database and can miss out all of the above steps...

There are lots of cases where other implementations of LINQ have proven very useful, but as yet I've to find a situation where LINQ to SQL has given me any advantage over what IO already have.

# re: Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

Thursday, April 24, 2008 10:17 AM by Guy

Thanks for the simple walkthrough, this was helpful.

# re: Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

Thursday, April 24, 2008 6:14 PM by ryansjedi

@ca8msm

You can use stored procedures with LINQ too.  That way you have the best of both worlds.

See: weblogs.asp.net/.../linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

# re: Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

Friday, April 25, 2008 3:37 AM by ca8msm

@ryansjedi

Thanks, I'm aware that you can do that...I just don't see the point. I have my logic in the SP to only return the records that I need and in the order that I want. The records are then bound to my custom objects which I can traverse through if I need to, so why do I need LINQ?

LINQ can be very useful, I just haven't come across any situation where LINQ to SQL has given me any advantage over what I already have. See my post here for an example of when LINQ is useful:

weblogs.asp.net/.../linq.aspx

# re: Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

Sunday, April 27, 2008 1:18 AM by albertpascual

@ca8msm

@ryansjedi

Thanks so much for opening such a good point. LINQ to SQL was develop to actually replace how you talk to databases I believe. Do you think that will be there in a few years? or that people won't move to it?

# re: Using LINQ to SQL makes life easier. An introduction of LINQ to SQL.

Monday, April 28, 2008 2:42 PM by ryansjedi

@ca8msm

The reason you would do it is that you can skip that one step in your code.  Instead of binding them in your own logic it will be done for you.  Say that your custom object is called person and has 2 members firstname and last name.  You can do this:

var mypeople = from p in ctx.PeopleTable select new Person{firstname=p.firstname, lastname=p.lastname}

Now you have a list of Person objects in one step.

My guess is that your method is a few more steps.  But it does come with a performance cost.  See this post for some interesting tests I ran today: weblogs.asp.net/.../linq-to-sql-speed-test.aspx

Leave a Comment

(required) 
(required) 
(optional)
(required)