Using LINQ to SQL - Part 1
page 4 of 6
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30419/ 53

LINQ to SQL Code Examples

Once we've modeled our database using the LINQ to SQL designer, we can then easily write code to work against it.  Below are a few code examples that show off common data tasks:

1) Query Products From the Database

The code below uses LINQ query syntax to retrieve an IEnumerable sequence of Product objects.  Note how the code is querying across the Product/Category relationship to only retrieve those products in the "Beverages" category:

Figure 3


Figure 4


2) Update a Product in the Database

The code below demonstrates how to retrieve a single product from the database, update its price, and then save the changes back to the database:

Figure 5


Figure 6


Note: VB in "Orcas" Beta1 doesn't support Lambdas yet.  It will, though, in Beta2 - at which point the above query can be rewritten to be more concise.

3) Insert a New Category and Two New Products into the Database

The code below demonstrates how to create a new category, and then create two new products and associate them with the category.  All three are then saved into the database.

Note below how I don't need to manually manage the primary key/foreign key relationships. Instead, just by adding the Product objects into the category's "Products" collection, and then by adding the Category object into the DataContext's "Categories" collection, LINQ to SQL will know to automatically persist the appropriate PK/FK relationships for me. 

Figure 7


Figure 8


4) Delete Products from the Database

The code below demonstrates how to delete all Toy products from the database:

Figure 9


Figure 10


5) Call a Stored Procedure

The code below demonstrates how to retrieve Product entities not using LINQ query syntax, but rather by calling the "GetProductsByCategory" stored procedure we added to our data model above.  Note that once I retrieve the Product results, I can update/delete them and then call db.SubmitChanges() to persist the modifications back to the database.

Figure 11


Figure 12


6) Retrieve Products with Server Side Paging

The code below demonstrates how to implement efficient server-side database paging as part of a LINQ query.  By using the Skip() and Take() operators below, we'll only return 10 rows from the database - starting with row 200.

Figure 13


Figure 14


View Entire Article

User Comments

Title: images not visible   
Name: Dhriti
Date: 2013-02-06 6:02:23 AM
images are not visible.
Title: Image Not Visible   
Name: Manish Prasad
Date: 2012-09-04 1:29:48 AM
Images are not Visible in the Article(, Please Do the Needful

Title: thanks buddy   
Name: pravy
Date: 2009-12-30 6:54:36 AM
very help full ..... upload some more articles
Title: Developer 1   
Name: SL
Date: 2009-09-29 7:21:21 AM
Good Article
Title: Developer   
Name: SL
Date: 2009-05-11 10:16:51 AM
Very short and simple understanding article for a novice. XCellent..

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-23 5:48:56 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search