Telerik UI for WPF

The purpose of this tutorial is to show you how to use LINQ against ADO.NET Entity Data Model. 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 ADO.NET Data Model.

 
Common Linq To Ado Net 010

Using the ADO.NET Entity Framework wizard you can easily create a representation of the sample Northwind database like the one below:

 
Common Linq To Ado Net 020

Query an Entity from the Database

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

CopyC#
NorthwindEntities dbContext = new NorthwindEntities();
var query = from p in dbContext.ProductSet
            where p.Categories.CategoryName == "Seafood"
            select p;
IEnumerable<Product> products = query.ToList();
CopyVB.NET
Dim dbContext As New NorthwindEntities()
Dim query = From p In dbContext.ProductSet _
    Where p.Categories.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#
NorthwindEntities dbContext = new NorthwindEntities();
Product product = dbContext.ProductSet.Single( p => p.ProductName == "Aniseed Syrup" );
product.UnitPrice = 1000;
dbContext.SaveChanges();
CopyVB.NET
Dim dbContext As New NorthwindEntities()
Dim query = From p In dbContext.ProductSet_
            Where p.ProductName = "Aniseed Syrup" _
            Select p
product.UnitPrice = 1000
dbContext.SaveChanges()

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#
NorthwindEntities dbContext = new NorthwindEntities();

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.AddToCategorySet( category );
dbContext.SaveChanges();
CopyVB.NET
Dim dbContext As New NorthwindEntities()

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.AddToCategorySet(category)
dbContext.SaveChanges()

Delete a Record from the Database

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

CopyC#
NorthwindEntities dbContext = new NorthwindEntities();
var query = from p in dbContext.ProductSet
            where p.ProductName.Contains( "Test" )
            select p;
foreach ( Product p in query )
    dbContext.DeleteObject( p );
dbContext.SaveChanges();
CopyVB.NET
Dim dbContext As New NorthwindEntities()
Dim query = From p In dbContext.ProductSet _
    Where p.ProductName.Contains("Test") _
    Select p
For Each p As Product In query
    dbContext.DeleteObject(p)
Next
dbContext.SaveChanges()

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#
NorthwindEntities dbContext = new NorthwindEntities();
var query = ( from p in dbContext.ProductSet
              where p.Categories.CategoryName == "Aniseed Syrup"
              select p ).Skip( 300 ).Take( 15 );
IEnumerable<Product> products = query.ToList();
CopyVB.NET
Dim dbContext As New NorthwindEntities()
Dim query = (From p In dbContext.ProductSet _
    Where p.Categories.CategoryName = "Aniseed Syrup" _
    Select p).Skip(300).Take(15)
Dim products As IEnumerable(Of Product) = query.ToList()

See Also