Introduction:
In this article,i am going to explain about what is linq programming,how it’s working and how
we can implement into asp.net projects.
Main:
Before starting,some important points about linq,
1.LINQ is a uniform programming model for any kind of data. LINQ enables you to query and manipulate data by using a consistent model that is independent of data sources.
2.LINQ is another tool for embedding SQL queries into code.
3.LINQ is another data abstraction layer.
What is Linq?
LINQ is a programming model that introduces queries as a first-class concept into any Microsoft .NET language. Complete support for LINQ, however, requires some extensions in the language you are using. These extensions boost developer productivity, thereby providing a shorter, more meaningful, and expressive syntax with which to manipulate data.
LINQ provides a methodology that simplifies and unifies the implementation of any kind of data access. LINQ does not force you to use a specific architecture; it facilitates the implementation of several existing architectures for accessing data, for example:
1.RAD/prototype
2.Client/server
3.N-tier
4.Smart client
Why Do We Need Linq?
Data managed by a program can be originated from various data sources: an array, an object graph, an XML document, a database, a text file, a registry key, an e-mail message, Simple Object Access Protocol (SOAP) message content, a Microsoft Office Excel file.
Each data source has its own specific data access model. When you have to query a database, you typically use SQL. You navigate XML data by using the Document Object Model (DOM) or XPath/XQuery. You iterate an array and build algorithms to navigate an object graph. You use specific application programming interfaces (APIs) to access other data sources, such as an Excel file, an e-mail message, or the Windows registry. In the end, you use different programming models to access different data sources.
The unification of data access techniques into a single comprehensive model has been attempted in many ways. For example, Open Database Connectivity (ODBC) providers allow you to query an Excel file as you would a Windows Management Instrumentation (WMI) repository. With ODBC, you use an SQL-like language to access data represented through a relational model.
Sometimes, however, data is represented more effectively in a hierarchical or network model instead of a relational one. Moreover, if a data model is not tied to a specific language, you probably need to manage different type systems. All these differences create an “impedance mismatch” between data and code.
LINQ addresses these issues by offering a uniform way to access and manage data without forcing the adoption of a “one size fits all” model. LINQ makes use of common capabilities in the operations in different data models instead of flattening the different structures between them. In other words, by using LINQ you keep existing heterogeneous data structures, such as classes or tables, but you get a uniform syntax to query all these data types regardless of their physical representation. Think about the differences between a graph of in-memory objects and relational tables with proper relationships. With LINQ you can use the same query syntax over both models.
How Linq Works?
Linq is called a query expression.It’s just a new feature in asp.net 3.0.
LINQ query syntax is more complete and articulated. Every query starts with a from clause and ends with either a select clause or a group clause. The reason to start with a from clause instead of a select statement, as in SQL syntax, is related to the need to provide Microsoft IntelliSense capabilities within the remaining part of the query, which makes writing conditions, selections, and any other LINQ query clauses easier. A select clause projects the result of an expression into an enumerable object. A group clause projects the result of an expression into a set of groups, based on a grouping condition, where each group is an enumerable object. The following code shows a prototype of the full syntax of a LINQ query expression:
query-expression ::= from-clause query-body
query-body ::=
join-clause*
(from-clause join-clause* | let-clause | where-clause)*
orderby-clause?
(select-clause | groupby-clause)
query-continuation?
from-clause ::= from itemName in srcExpr
select-clause ::= select selExpr
groupby-clause ::= group selExpr by keyExpr
The first from clause can be followed by zero or more from, let, or where clauses. A let clause applies a name to the result of an expression, while a where clause defines a filter that will be applied to include specific items in the results. Each from clause is a generator that represents an iteration over a sequence on which query operators (such as the extension methods of System.Linq.Enumerable) are applied.
let-clause ::= let itemName = selExpr
where-clause ::= where predExpr
A from clause can be followed by any number of join clauses. The final select or group clause can be preceded by an orderby clause that applies an ordering to the results:
join-clause ::=
join itemName in srcExpr on keyExpr equals keyExpr
(into itemName)?
orderby-clause ::= orderby (keyExpr (ascending | descending)?)*
query-continuation ::= into itemName query-body
Linq Implementations
LINQ is a technology that covers many data sources. Some of these sources are included in LINQ implementations that Microsoft provides as part of the .NET 3.5 Framework,
Each of these implementations is defined through a set of extension methods that implement the operators needed by LINQ to work with a particular data source. Access to these features is controlled by the imported namespaces.
LINQ to Objects
LINQ to Objects is designed to manipulate collections of objects, which can be related to each other to form a graph. From a certain point of view, LINQ to Objects is the default implementation used by a LINQ query. LINQ to Objects is enabled by including the System.Linq namespace.
for ex,
string tempPath = Path.GetTempPath();
DirectoryInfo dirInfo = new DirectoryInfo(tempPath);
var query =
from f in dirInfo.GetFiles()
where f.Length > 10000
orderby f.Length descending
select f;
string tempPath = Path.GetTempPath(); DirectoryInfo dirInfo = new DirectoryInfo(tempPath); var query = from f in dirInfo.GetFiles() where f.Length > 10000 orderby f.Length descending select f; |
LINQ to ADO.NET
LINQ to ADO.NET includes different LINQ implementations that share the need to manipulate relational data. It also includes other technologies that are specific to each particular persistence layer:
LINQ to SQL Handles the mapping between custom types in .NET and the physical table schema.
LINQ to Entities Is in many ways similar to LINQ to SQL. However, instead of using the physical database as a persistence layer, it uses a conceptual Entity Data Model (EDM). The result is an abstraction layer that is independent from the physical data layer.
LINQ to DataSet Makes it possible to query a DataSet using LINQ.
LINQ to SQL and LINQ to Entities have similarities because they both access information stored in a relational database and operate on object entities that represent external data in memory. The main difference is that they operate at a different level of abstraction. While LINQ to SQL is tied to the physical database structure, LINQ to Entities operates over a conceptual model (business entities) that might be far from the physical structure (database tables).
Linq to XML:
LINQ to XML offers a slightly different syntax that operates on XML data, allowing query and data manipulation. A particular type of support for LINQ to XML is offered by Visual Basic 2008, which includes XML literals in the language. This enhanced support simplifies the code needed to manipulate XML data.
for ex,
Conclusion:
Hope this helps,
Happy coding.
time ASP.Net Programmer to join our Creative Services team. Article Directory
hello
I’ve recently started a blog, the information you provide on this site has helped me tremendously. Thank you for all of your time & work.
I’ve bookmarked your website, just because I came acrossit and it really seems to be ambitiousand I enjoyed studing your posts.
This article resolved my difficulties, Thanks a lot.
Let me start by saying appreciable blog. I shall submit some points which are the greatest parts of knowledge in our opinion. This may help you bringing more info for us.
Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.