LINQ Entity Framework

Microsoft has released it’s Entity Framework to the work, so does this mean the death of LINQ to SQL?  Check out my blog on this, Death of LINQ to SQL

So why is LINQ so good, well the problems with data has always been around the different and many way you need to know to interface and programme to.  For example if you conntect to a Microsoft SQL Server database, you need to know SQL, to read and write to XML files you need to learn XPath or XQuery, to query Active Directory you need to know LDAP (Lightweigth Directory Access Protocol).  To know all of these are more is quite a task.

SO along comes LINQ, which is a intergrated query language, which provides the same query language and grammer features, no matter where the data is held.

Microsoft started off by allowing developers to use LINQ to XML, LINQ to L, and LINQ to Objects.  These are all good starting blocks, but I have always been aware that Microsofts intention has been to pull all of these together and under one method, and that is the Entity Framework.  Which is built on top of LINQ and ADO.NET 3.0.

In order to use the Entity Framework you will nedd Visual Studio 2008, along with Service Pack 1 and .NET 3.5

Defining an Entiry Data Model

Once you have created your project, say an Console Application, you can get down to generating an Entity Data Model


  1. Make sure you have Visual Studio 2008 SP1, otherwisethe Entity Framework will not be available.
  2. Add a New Item to your project, and select ADO.NET Entity Data Model.
  3. Change the name to read NorthwindModel.edmx
  4. Select to Generate From Database
  5. Choose you data connection you wish to use, select the Northwind database
  6. Save the connection, you should find your app.config should now have the NorthwindEntities
  7. Select the database objects you wish to choose, in ower example select Customer, Orders, Order Details and Products.
  8. Change the Model Namespace to NorthwindModel and click finish.
You should end up with an .EDMX file looking something like this:

Okay, now it is time to query the data

Querying Products

What is nice about LINQ is it looks and feels very much like SQL.  With one exception, you have to write the syntax back to front.  Why I hear you ask, I don’t know the official answer to this, but my thoughts are that it is due to intellisense, as by writing you syntax backwards the intellisense can find the information you need.

Okay to query the Products table, you will first need to create an instance of the NorthwindEntities Class, where did this come from?  Well it was created when you created the .edmx file.


NorthwindEntities entities = new NorthwindEntities();

Dim db As New NorthwindEntities()


Next we have to create the LINQ statement to read the Products table / entity


var products = from p in entities.Products

                           where p.ProductID == 7
                           select p;

Dim products = From p In entities.Products _
                       Where p.ProductID = 7 _
                       Select p

Okay now you have the products you can loop through the list by using a For Each method
foreach (var p in products)
For Each p In products
How simple could it be?
Check out my other blog entries in using LINQ to find out more information on what you can do with LINQ

To find out more about LINQ and the Entiry Framework check out The ADO.NET Entity Framework OverviewThe ADO.NET Entity Framework Overview or Introduction to ASP.NET Dynamic Data