Develop a simple application using LINQ [Part 1]

Published Date
10 - Nov - 2011
| Last Updated
10 - Nov - 2011
 
Develop a simple application using LINQ [Part 1]
In the previous article titled "LINQ - An easy introduction" we saw how LINQ works with data and objects. In this article which is a part of a series, we will develop a very simple inventory application for a grocery store (let's call is Evergreen Store). Over the next five articles in this series, we'll see the basic operations in LINQ which are required to build any application. The functions here are very simple and can be modified and optimised as you learn more advanced programming in LINQ. The purpose of this article is to provide explanation of the LINQ coding implemented as the application is being built.
 
The database which we will use is SQL Server although it won’t matter much as the front end LINQ programming won’t change even if we switch databases without changing the table structure (more on that later).
 
In this part of the series we will look at the application’s functions, the LINQ operations covered in this series, the database composition, LINQ to SQL Class Designer and the functional division of the GUI. 
 
Applications Functions:
1. Display all items.
2. Product search functionality
3. Insert, Delete & Modify product data.
4. Display products which are at/past the reorder level.
 
 
Viewing the inventory in Evergreen Store
 
Add/modify the inventory
 
LINQ operations in this Application:
1. Using the LINQ to SQL Class Designer.
2. Binding data retrieved by LINQ to form controls.
3. Looping through multiple records obtained through LINQ.
4. Use of “var” keyword.
5. Retrieving all records.
6. Conditional retrieval of records.
7. Adding records.
8. Deleting records
9. Modifying records.
 
 
Database Name
Inventory
 
SQL Server Database Tables
1. Products
 
Products Table Columns
1. Product_ID
- Primary Key
2. Product_Name
3. Product_Category
4. Product_Price
5. Product_Quantity
6. Product_ReorderLevel
- Quantity level after which a product must be restocked.
 
 

LINQ to SQL Class Designer

Did you wonder why it was so easy to manipulate SQL Server Data in LINQ and why the front end code will remain the same even if the back end source changes? It is all possible thanks to LINQ-to-SQL Classes which are nothing but simple Database Markup Language (dbml) files. This DBML file contains the schema of the database elements (tables, functions, procedures, etc). Here, we will only be working with tables as the purpose of this article is to give a basic introduction to LINQ.
 
The table can simply be dragged from the server explorer into the LINQ to SQL Designer window
 
 
Add a new LINQ-to-SQL File to the project and name it "LINQ_Inventory". After adding the file the LINQ-to-SQL Class Designer opens up to which you can simply drag the "Products" table from the database in the Server Explorer into it. 
Adding the Products table to the designer generates a datacontext and entities for the Product table in the designer. In the same manner functions and stored procedures can be added to the designer which will result in classes being generated for them which will also have representations of the function's result. Since we are keeping the application simple, we will only work with tables here.
 

Building the GUI

Basically we have a one form simple application which will divide the functionality by using a tab control with two tabs. In the first tab "View & Search" we will have the search, view all and view reorder products functionality. The other tab, "Add/Delete" will have the add, delete and modify product functionality.
 
In the next part of the series we will begin LINQ coding and implement the functionality of the first tab "View & Search".
Sanket ChaukiyalSanket Chaukiyal