Skip to main content

Introducing LINQ Queries in ASP.NET

Language Integrated Query (LINQ) is a new component of .NET Framework. The basic function of LINQ is to add native data querying capabilities to .NET Framework by using syntax similar to Structured Query Language (SQL). LINQ allows you to define statements that query a data source to generate the requested result set.
LINQ is an attempt to provide a consistent method to obtain and manipulate data. You can use LINQ directly within the ASP.NET programming language entities called query expressions. These query expressions are based on numerous query operators that have been designed to work in a manner similar to that of SQL. LINQ defines the set of query operators, which are used to query and filter the data. The difference between LINQ and SQL is that that unlike SQL, the query expressions in LINQ can be used to interact with numerous types of data, even the data that does not belong to a relational database.

A query is an expression that retrieves the requested data from a data source. A LINQ query specifies the information that you want to retrieve from a data source. LINQ queries can also perform additional functions, such as sorting and grouping the retrieved data . The LINQ queries are written in declarative query syntax. The various clauses that you can use with the LINQ query are the From, Where, OrderBy, and Select clauses. They are the predefined clauses that you can use for the execution of a LINQ query.
The basic syntax of using a LINQ query is that the LINQ query expressions must start with the From clause and end with the Select or the Group clause. You can use the Where and OrderBy clauses to perform additional functions such as retrieving the data.

The following are three basic steps of a LINQ query execution:

1.  The first step in the LINQ query execution is to obtain the data source. The data source can be either a SQL database or an XML file.
2.  The second step is to create the query.
3.  Lastly, you need to execute your LINQ query.

Before you work with LINQ queries, you need to be aware of the following basic concepts:

□ Data Sources in LINQ Queries
□ Deferred Query Execution and Immediate Execution
□ LINQ and Generic Types

Data Sources in LINQ Queries

The data source in a LINQ query can be a data structure, a Web service, a file system, or a database. LINQ query execution is simple because the syntax and the pattern of the query do not change with the change in the data source. The different ways in which you can use LINQ with different data sources are as follows:
□ You should implement the IEnumerable<T> interface for enabling the LINQ to Objects querying.
□ You can create standard query operator methods, such as Where and Select, to enable custom LINQ queries
□ You can also create a provider for your data source that implements the IQueryable<T> interface to enable LINQ queries. A provider that implements this interface receives LINQ queries in the form of expression trees.
□ You can also create a provider that enables not only querying but also insert, update, and delete operations.

When you retrieve data using LINQ, the data source is not important. Execution of LINQ queries are independent of data types of data and structure of original data source.
LINQ previews the data source as an IEnumerable<T> or IQueryable<T> collection. For example, in LINQ to SQL, the source data is made visible as an IEnumerable<XElement> collections element. In LINQ to DataSet, the source data is made visible as an IEnumerabie<DataRow> collections."

Deferred Query Execution and Immediate Execution

The LINQ query only stores query commands. When vou define a query in LINQ during runtime, the query does not run. The query runs when the items are iterated. The query variable only stores the query commands. The actual execution of LINQ query is held back until you iterate over the query variable in a ForEach statement. This concept of holding back the actual execution is termed as deferred query execution. You can use the ForEach statement to retrieve the LINQ query results. For example, in a database that is updated continuously, you can create a LINQ query that retrieves the latest data, and you can also execute it repeatedly at different intervals to retrieve different results every time.

If the queries perform aggregation functions over a range, the elements must first iterate over the range. Ihis is called immediate execution. These functions must first iterate over the source elements and then display the result. The clauses that perform such kind of forced execution are Count, Max, Min, and Average. Unlike deferred execution, immediate execution of queries does not require an explicit ForEach statement to return the result.

If you compare deferred to immediate query execution, deferred query execution has an edge over the immediate query execution. This is because in deferred query execution the query does not hold the result. You can execute it as often as you like. You can also update the data source on a regular basis by a separate application. You can retrieve the latest data in deferred query execution. This kind of functioning is not possible in immediate query execution.

LINQ and Generic Types

The LINQ queries are based on generic types. These types were introduced with .NET Framework 2.0. The generic types are used to maximize code reuse, type safety, and performance. The most common use of generic is to create collection classes. There are two basic concepts of the generic types used in LINQ:
1.  In a LINQ query, when you create an instance of a generic collection class, such as List<T>, you replace the T with the type of objects that the list will hold. For example, a list of strings is represented as List<String> and a list of Customer object is represented as List<Customer>. A generic list provides many benefits over collections that store their elements as Objects. For example, if you try to add a Customer to a List<String>, you will get a compile-time error. In such cases, it is easy to use generic collections as you do not need to perform run-time type-casting.
2.  The second generic type that LINQ uses is the IEnumerable<T> interface. It enables the generic collection classes to be enumerated by using the ForEach loop. For example, a LINQ query variable that is typed as IEnumerable<Customer> means that when the query is executed it will produce a sequence of zero or more Customer objects.


Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video

Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:

      Suppose next node is allocated at an address 506, so the list becomes,

  Suppose next node is allocated with an address with an address 10,s the list become,

The other way to represent the linked list is as shown below:

 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …