Language Integrated Query (LINQ): Part 4

This is fourth part of the "LINQ" series of articles that I have started from here. And in this article, you will learn how to setup a demo project to explore LINQ queries. I will use my preinstalled "Northwind" database that you can download from here and install it in SQL Server Management Studio.


Video here:



Now, follow the steps to setup a demo project to explore LINQ queries. Before creating the new project, setup a "Northwind" database in SQL Server Management Studio.




Step 1: Creating the New Project


Open the Visual Studio instance and create a new project New > Project > Console Application and name the project whatever you wish.


Step 2: Adding LINQ to SQL Classes


Now, in the Solution Explorer add a new item "LINQ to SQL Classes" using a nice name like "DataNorthwind.dbml".



This will add all required libraries as well as classes to your project for database communication. Actually, this DBML file will become a layer between the DB and your project and provide you all the IntelliSense features.




Step 3: Adding New Connection


Open "Server Explorer" and add a new SQL Server connection, as in the following image:




Once you are done with the above, drag the tables from the "Server Explorer" to the "DataNorthwind.dbml" file designer; see:




Now, you will get following DB diagram in the designer:




You will notice the following things here:

  1. A new connection string in the App.config file.
  2. Every setup required to communicate with the DB like DataNorthwindDataContext, TableAttribute, EntitySet etc in was created in the DataNorthwind.designer.cs file.

Step 4: Coding and Running Project


Now, setup the project code as given below and run it.




We already saw this code in one of the previous articles. Now, we have a demo project setup completely and ready to try all the LINQ queries.


In an upcoming article, we will look at various LINQ queries and will test it using the preceding project.


I hope you will find it useful. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all