Home > Programming Concepts > Application Development Using LINQ Part – 2

Application Development Using LINQ Part – 2

In the previous article we have learned much about the LINQ and its advantage over normal SQL statements, and to know the working of LINQ we are creating an Inventory Application of which GUI we have created. Now, in this part we will be looking at datacontext, the “var” keyword, retrieving data through LINQ, binding retrieved data to controls and conditional retrieval of data. You’ll also begin to see some LINQ coding. We’ll begin with the controls in the “View & Search” tab of the tab control. Now we will start coding for the first tab ‘View & Search’ of this application.

The Data Context

When we had created our LINQ_Inventory file, a datacontext was created called “LINQ_InventoryDataContext” which is a class that provides access to the database tables and functions that can be performed on them. Let’s create an instance of this class which will enable us to access the functionality of the datacontext. We will be using it throughout the code to access the Products table data.

LINQ_InventoryDataContext InventoryDataContext = new LINQ_InventoryDataContext();

Now we will see how easy it is to retrieve data using LINQ and even easier to bind it to a datagrid control.

private void btnShowAllProducts_Click(object sender, EventArgs e)
{
	var products = from a in InventoryDataContext.Products
		                select a;            
        dataGridView_Products.DataSource = products;
}

As you can see, we have extracted data from the products table and bound it to the data grid control in just two short lines of code within the “Click” event of the “All Products” button. The “var” keyword is used because we are retrieving variable data instead of a fixed type (in this case we are recovering multiple rows with multiple and not just a single datatype). As you can see we are accessing the “Products” table using the data context “InventoryDataContext” (we can access any table in a database this way depending on the tables we drag into the LINQ-to-SQL Class Designer from the database). The LINQ statement above is the same as “Select * from Products” in SQL.

Now that we have retrieved all the records in the Products table, we simply bind the “products” variable to DataSource property of the datagrid and we are done. It’s that simple.

Let’s move on to the “Products for Reorder” button where the only difference is that the LINQ statement is conditional. First a little about Reorder Quantity. This refers to the stock of a product in the inventory below. If a product stock declines it must be reordered to maintain continuous availability.

private void btnReorderProducts_Click(object sender, EventArgs e)
{
	var products = from a in InventoryDataContext.Products
				where a.Product_Quantity<=a.Product_ReorderQuantity                           
				select a; 
         dataGridView_Products.DataSource = products;
}

Here, in the LINQ statement we are comparing the current quantity of a product to its reorder level to determine whether it needs to be reordered. In SQL, this statement would translate to “Select * from Products where Products_Quantity<=Products_ReorderQuantity". We bind the result of the LINQ statement to the data grid view in the same way as before (by binding the "products" variable to the DataSource property of the data grid.

Next we have the "Search" button which retrieves records based on the search parameters. Here, we have deliberately kept the search a simple affair since an advanced search is beyond the scope of this article. We write the search code within the "Click" even of the "Search" button which will take the search parameters from the search textbox (txtSearch).

private void btnSearch_Click(object sender, EventArgs e)
{
	var products = from a in InventoryDataContext.Products
				where a.Product_Name.Contains(txtSearch.Text) == true  || a.Product_Category.Contains(txtSearch.Text) == true || Convert.ToString(a.Product_Price).Contains(txtSearch.Text) == true
	   		        select a; 
        dataGridView_Products.DataSource = products; 
}

In this code we are simply looking whether the search string or any part of it occurs within the Product Name, Product Category or Product Price columns of the Products table. The “a.Product_Name.Contains(txtSearch.Text)” method simply checks whether the current record in the Column “Product_name” contains any part of the string passed in “txtSearch.Text” and returns a (boolean)true/false value. The “||” in the LINQ query is represents an “OR” in SQL.

In the next article we will move on to the controls present on the “Add/Delete” tab of the tab control.

I am Samith Jhon a content writer and a Professional Blogger. Certified with 1Z0-515 Exam test questions which is very
popular these days and have a great scope in the field of IT Certification. I always like to take 1Z0-528 Exam test questions to pass the Exam. These kinds of Exam questions could secure your future as well as your job.

176aff16f5910279fc4a958a7cd12be6
Share via email Share

About amitrko

An Internet World Geek, hooked on all things pertaining to Web Design and Development & Online Magazine, intent on delivering you the best in Web content to simplify your Blogging Experience.

3 comments

  1. Why do you do this:

    Convert.ToString(a.Product_Price).Contains(txtSearch.Text) == true

    When “Contains” is a boolean? Why not just leave out the “== true”?

Leave a Reply