Introduction and Goal
In this section we will run through basics of LINQ and then see 5 basic LINQ queries which you will always need in your project for queries. While looking at the basics we will also try to learn what problem LINQ solves from the perspective of middle tier business objects.
Define LINQ?
LINQ is a uniform programming model for any kind of data access. LINQ enables you to query and manipulate data independently of data sources. Below figure 'LINQ' shows how .NET language stands over LINQ programming model and works in a uniformed manner over any kind of data source. It's like a query language which can query any data source and any transform. LINQ also provides full type safety and compile time checking.
LINQ can serve as a good entity for middle tier. So it will sit in between the UI and data access layer.

Figure - LINQ
Below is a simple sample of LINQ. We have a collection of data 'objcountries' to which LINQ will is making a query with country name 'India'. The collection 'objcountries' can be any data source dataset, datareader, XML etc. Below figure 'LINQ code snippet' shows how the 'ObjCountries' can be any can of data. We then query for the 'CountryCode' and loop through the same.

Figure: - LINQ code snippet
How does LINQ help us from the perspective of business objects?
One of the tedious jobs in business object is parsing and searching object collections. For instance consider the below figure where we want to search a country by an 'ID' value. So what we do is loop through the collection and get the object. Many may argue how about keeping a key in List or Array. The below example is just a sample. For instance if you want to search using country code and name, list / collection keys will not work with those multi-value searches.

In other words using LINQ we can query business object collections and filter the collection in a single LINQ query.
Can you explain how a basic LINQ Query looks like?
In order to understand the basic query for LINQ, let's make a small sample project. Let's take customer data which has customer and orders.
| Customer Name | Customer Code | City | Orders |
| Khadak | 001 | Mumbai | Shirts Socks |
| Shiv | 002 | Delhi | Pants |
| Raju | 003 | Mumbai | Socks |
| Shaam | 004 | Delhi | Shoes |
We have made the data a bit complex by have one customer and multiple orders , in other words we have one as to many relationship.

So let's make two classes one is the customer class aggregated with a collection of addresses class. Below is how the class structure will look like to accommodate the one as to many relationships of customer and multiple addresses.

The multiple addresses are the array collection aggregated inside the customer class. So below is the code snippet which is loading the customer and address collections with hard coded data provided in the above table. Currently its hardcoded but this can be loaded from database or some other source also.







Join the conversation! Your thoughts help the community grow.