RadControls for WPF

The purpose of this tutorial is to show you how to use LINQ to SQL Classes. LINQ to SQL is an ORM (object relational mapping) implementation, which allows you to model a relational database using .NET classes. You can then query the database using LINQ, as well as update\insert\delete data from it. LINQ to SQL fully supports transactions, views, and stored procedures.

This topic will cover how to:

Tip
For the purpose of this tutorial the Northwind database will be used, which can be downloaded from here

Add a new LINQ to SQL mapping.

 
Common Linq To Sql 010

Using the LINQ to SQL designer you can easily create a representation of the sample Northwind database like the one below:

 
Common Linq To Sql 020

Query an Entity from the Database

The next code-snippet uses LINQ query syntax to retreive an IEnumerable sequence of Product objects.

CopyC#
NortwindDataContext dbContext = new NortwindDataContext();
var query = from p in dbContext.Products
               where p.Category.CategoryName == "Seafood"
               select p;
IEnumerable<Product> products = query.ToList();
CopyVB.NET
Dim dbContext As New NortwindDataContext()
Dim query = From p In dbContext.Products _
    Where p.Category.CategoryName = "Seafood" _
    Select p
Dim products As IEnumerable(Of Product) = query.ToList()

Update an Entity in the Database

The code below demonstrates how to grab a single Product object from the database, update its price, and then save the changes back to the database.

CopyC#
NortwindDataContext dbContext = new NortwindDataContext();
Product product = dbContext.Products.Single( p => p.ProductName == "Aniseed Syrup" );
product.UnitPrice = 1000;
dbContext.SubmitChanges();
CopyVB.NET
Dim dbContext As New NortwindDataContext()
Dim query = From p In dbContext.Products _
            Where p.ProductName = "Aniseed Syrup" _
            Select p
product.UnitPrice = 1000
dbContext.SubmitChanges()

Insert a New Record(s) in the Database

The code below shows you how to create a new Category object. Then how to create two new Products and associate them with the Category. Finally, all three objects are saved in the database.

CopyC#
NortwindDataContext dbContext = new NortwindDataContext();

Category category = new Category();
category.CategoryName = "Test Category";

Product firstProduct = new Product();
firstProduct.ProductName = "Test Product 1";

Product secondProduct = new Product();
secondProduct.ProductName = "Test Product 2";

category.Products.Add( firstProduct );
category.Products.Add( secondProduct );

dbContext.Categories.InsertOnSubmit( category );
dbContext.SubmitChanges();
CopyVB.NET
Dim dbContext As New NortwindDataContext()

Dim category As New Category()
category.CategoryName = "Test Category"

Dim firstProduct As New Product()
firstProduct.ProductName = "Test Product 1"

Dim secondProduct As New Product()
secondProduct.ProductName = "Test Product 2"

category.Products.Add(firstProduct)
category.Products.Add(secondProduct)

dbContext.Categories.InsertOnSubmit(category)
dbContext.SubmitChanges()

Delete a Record from the Database

The code below demonstrates you how to delete all "Test" products from the database.

CopyC#
NortwindDataContext dbContext = new NortwindDataContext();
var query = from p in dbContext.Products
            where p.ProductName.Contains( "Test" )
            select p;
dbContext.Products.DeleteAllOnSubmit( query );
dbContext.SubmitChanges();
CopyVB.NET
Dim dbContext As New NortwindDataContext()
Dim query = From p In dbContext.Products _
    Where p.ProductName.Contains("Test") _
    Select p
dbContext.Products.DeleteAllOnSubmit(query)
dbContext.SubmitChanges()

Retreive a Record with Server Side Paging

The next example shows you how to implement efficient server-side database paging. By using the Skip() and Take() methods, you will return 15 rows from the database - starting with row 300.

CopyC#
NortwindDataContext dbContext = new NortwindDataContext();
var query = ( from p in dbContext.Products
              where p.Category.CategoryName == "Aniseed Syrup"
              select p ).Skip( 300 ).Take( 15 );
IEnumerable<Product> products = query.ToList();
CopyVB.NET
NortwindDataContext dbContext = new NortwindDataContext();
Dim query = (From p In dbContext.Products _
            Where p.ProductName == "Aniseed Syrup" _
            Select p).Skip(300).Take(15)
Dim products As IEnumerable(Of Product) = query.ToList()

See Also