linq query - language integrated query
LINQ stands for Language Integrated Query. It is an important feature of Visual Studio � the RAD tool from Microsoft used for developing applications for various requirements such as Accounting, Inventory and ERP domains. All the languages of Visual Studio - C# & VB.Net support LINQ. The reason why it has become popular because, it enables us to query data natively.
With this technology, we can use C# or VB.net queries from with in an ERP application program to query data. The syntax of such a query is similar to SQL. For instance, the following statements are LINQ queries in C# and VB.Net. In C#, queries must begin with from clause and end with a select or group-by clause as shown below.
Query written in c#
var queryByCountry =
from cust in customers
group cust by cust.Country
select cust;
Query written in vb
Dim queryByCountry =
From cust In customers
Group cust By cust.Country
Select cust
In a single query, we can include a number of data sources such as array, list, queue, dictionary and database. Linq provides a set of query operators that are available both in vb and c# as keywords. These query operators are used to perform various operations. Types of linq queries are:
A query written to query the in-memory objects is called linq to objects query.
A query written to query the database is called linq to sql query.
A query written to query the dataset is called linq to dataset query.
Observe that, the query pattern is consistent in both queries. Also, note that query patterns are consistent when written to extract data from different data sources such as collections, XML documents and databases.
The purpose of introducing Linq by Microsoft is to enable programmers write English like query statements which can be used to access varied data stores. LINQ was introduced in the 2008 version of VS and 3.5 version of .Net framework. This querying technology makes it easy for programmers to work with applications which have objects and data. Queries written using this technology can be placed inside Vb or C# code as language constructs. For existing projects also, the new Linq queries can be written which can work along with existing C# or VB code.
Some applications require to store data in different formats such as relational databases ( SQL server database, Oracle), XML documents and data in objects and so on. This technology enables a programmer to access data in the above formats within a C# or VB application without having a need to learn the querying technology or syntax of a particular data store.
Suppose that we are developing an ERP app using C#. There will be many instances when we would like to retrieve data from an array or from database tables or collection. In such a scenario, we can use a Linq query such as the one given above.
Observe that in the above code when we initialize a variable we use var modifier instead of specifying a type. C# supports initializers for objects and collections which eliminates the need to call a constructor for the object. C# also provides anonymous types which enable a programmer to group properties in a query result for the specific purpose of that result only. C# includes extension methods which can we associated with a type and called as an instance method. With this feature we can add new methods without modification. Note that, at this point of time support for LINQ is extensive in C# as compared to VB.net.