Application Development Using LINQ

Language-Integrated Query or LINQ is another way to query data using native code. It adds native querying capability to .NET languages. You may ask “what is the need of another data access method when you already have a good one like ADO.Net?” Well, one of its major advantages is that, it is a general-purpose query language, which essentially means that it is not just used to query relational databases but also XML, Controls and Memory Data Objects. Curious? Let me show you how. Consider the following examples:

SQL Statement in ADO.NET

	Select * from Inventory where Price >50; 

LINQ statement

var product=from d in Inventory
	where d.Price >50
	select d.ProductName;

Simple enough right? But you’d wonder, what’s the advantage in using LINQ instead of an SQL statement. Consider this, in ADO.NET you will pass the SQL statement as a string command but in LINQ you are retrieving data using native code which has the benefit of intellisense. This ensures that there are no errata and all the other issues related with passing raw SQL commands as strings are resolved.

By using ‘var’ we let the compiler “guess” the type of variable we want. Now, if you say that we could have directly used “string” in the above example then you are right. But take a scenario where we want to recover all the data for the given product which contains integers as well as string, then we have no option but to use “var” with which we retrieve all types of data.

Window Form Controls

Imagine that you have a data entry form where all the text fields are supposed to be filled. Here is a simple way to test if the text fields are filled.

var FormTextBoxes = from MyControl in this.Controls.OfType<TextBox>()
							where MyControl is TextBox
							select MyControl;
foreach(TextBox textbox in FormTextBoxes)
      if (textbox.Text==String.Empty)
            textbox.Text = "Please enter data";
            textbox.BackColor = Color.Aqua;

Well, that was a simplistic code but you get the drift of what is possible and how it can drastically shorten your code.

Memory Data Object:

int[] age = { 45, 23, 77, 87, 12, 09, 86 };

IEnumerable<int> query = from i in age
where i > 77
	select i;

foreach (int item in query)

LINQ makes access to data memory objects as easy as the snap of your fingers. It treats the memory data object as just another data source and accesses it just as easily as it accessed an SQL data source or a collection of controls in a Windows form.

XML Data Source

LINQ also simplifies access to an XML data source. Consider the following XML document MyContacts.xml:


Now see the LINQ code used to access the XML data. Plain as vanilla.

XDocument ContactDocument = XDocument.Load(@"C:MyContacts.xml");

var q = from c in ContactDocument.Descendants("contact")
where c.Element("city").Value == "Hyderabad"
select c.Element("firstName").Value;

foreach (string name in q)

Wasn’t that really simple? RSS feed access would be an interesting and useful application. The data source will change from an XML document to an RSS feed (which is XML as well) but the code for traversing through XML data will remain the same.

LINQ is really great for rapid application development and can be learned relatively quickly; although, by no means is it a complete replacement for other data access methods. Complex operations such as locking, serialization and transaction management are best performed in SQL.

LINQ really makes it simple to query just about anything that holds data in .NET, and it does so using the same syntax with a little variation for different data sources. Although the learning curve for LINQ is quite simple considering all the examples you saw above, it can perform more powerful functions as well.

We have learned how LINQ is beneficial for our applications. So let’s move further in LINQ development, today, we will discuss the application development using the LINQ technology, for that we will develop an ‘Evergreen Store’ or grocery store inventory system. It might be difficult for the most of the readers out there, but trust me LINQ makes things better. The functions here are very simple and can be modified and optimized 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.

We will use the SQL server for the database creation, and this doesn’t matter which database engine you are using even if we switch databases without changing the table structure, because front end LINQ coding won’t change at all. So here we are using MS SQL Server Express. In this article, we will look at the application’s functions, the LINQ operations covered, the database composition, LINQ to SQL Class Designer and the functional division of the GUI.

Applications Functions:

Display all items.
Product search functionality
Insert Delete & Modify product data.
Display products which are at/past the reorder level.

LINQ operations in this Application:

Using the LINQ to SQL Class Designer.
Binding data retrieved by LINQ to form controls.
Looping through multiple records obtained through LINQ.
Use of “var” keyword.
Retrieving all records.
Conditional retrieval of records.
Adding records.
Deleting records
Modifying records.

Database Name


SQL Server Database Tables


Products Table Columns

-Primary Key
– 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.

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 – add, delete and modify product functionality.

We will discuss the coding part of this inventory application in the next part of the article, till then try to learn the basic concept of the LINQ and create some small GUI for using LINQ in ASP.NET.

I am Samith Jhon a content writer and a Professional Blogger. Certified with 1Z0-451 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-514 Exam test questions to pass the Exam. These kinds of Exam questions could secure your future as well as your job.

Share via email Share

Leave a Reply