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

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

C#:

Figure 4

VB:

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

C#:

Figure 6

VB:

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

C#

Figure 8

VB:

4) Delete Products from the Database

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

Figure 9

C#:

Figure 10

VB:

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

C#:

Figure 12

VB:

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

C#:

Figure 14

VB:


View Entire Article

User Comments

Title: images not visible   
Name: Dhriti
Date: 2013-02-06 6:02:23 AM
Comment:
images are not visible.
Title: Image Not Visible   
Name: Manish Prasad
Date: 2012-09-04 1:29:48 AM
Comment:
Images are not Visible in the Article(http://aspalliance.com/articleViewer.aspx?aId=1330&pId=-1), Please Do the Needful

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






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


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-06-19 1:07:10 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search