Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

What is LINQ in ASP.NET?

Author: Rudy Pinson

LINQ was introduced in .NET Framework 3.5. In this article, I will explain LINQ and LINQ providers. At the end we will also see code snippet to understand LINQ.

 

What is LINQ?

Language Integrated Query (LINQ) is a programming methodology that defines standard query operators for richer query capabilities. It is called Language Integrated Query because the query is the integrated feature of the .NET languages. It provides an expressive and declarative way to interact with data. It also provides a unified syntax to interact with any data source including in-memory collection, SQL database and XML file. LINQ allows SQL-Like syntax approach for querying various data sources. 
 
LINQ extends the data interaction capabilities into .NET languages. Data manipulating syntax is now part of the C# or VB.NET. That means we don’t need to write separate SQL queries for data manipulation. LINQ provides the facility to write queries in C# or VB.NET for SQL Server database data manipulation. So LINQ simplifies the way to interact with data sources by providing query syntax as first class language construct in .NET languages. It provides the consistent query syntax for XML, SQL database and objects.
 
LINQ has some advantages. First of all LINQ provides the syntax that can be used with different data sources. It means LINQ can be used for any data source and for any .NET supported language. This advantage of LINQ leads us to another advantage that is the short code as compared to SQL quires. Sometimes we need to write long SQL queries for data manipulation but the code can be less using LINQ which reduces the complexity.

When interacting with different data sources, we need to learn query languages for that data sources. With LINQ, we don’t need to learn specific query languages for data sources. Furthermore, LINQ is the part of the .NET language so it has the facilities of type checking, compiling and IntelliSence support.

 

LINQ Providers
 
LINQ is divided into different areas that are called LINQ providers.
 
LINQ to Objects
 
LINQ to object provider is used for querying in-memory collections by using local query execution engine. It allows us to use LINQ queries with any IEnumerable collection. We can use it with any IEnumerable collection whether it is user defined or it is defined in .NET Framework.
 
LINQ to XML
 
LINQ to XML is formally called XLINQ.  It is the provider to perform queries against XML data source. LINQ to XML provider is used to convert XML document to XElement objects and these XElement objects are then queried against using local search engine.
 
LINQ to SQL
 
LINQ to SQL is formally called DLINQ. LINQ to SQL provider is used to query SQL Server databases. SQL server has its own query engine so LINQ to SQL provider doesn’t use local search engine instead the LINQ is converted to SQL query first and SQL query engine process the query. LINQ works with data encapsulated in objects and SQL server stores data as relational data. LINQ to SQL mapping framework is used to map these two. Classes are made by using database tables and column name or part of the column name is used as data members.  
 
LINQ to DataSets
 
LINQ to DataSets are sometimes called LINQ to ADO.NET. LINQ to SQL provider works only with SQL Server databases. LINQ to DataSets uses ADO.NET to work with databases. LINQ to DataSets execute queries when data is in dataSets. It makes easier and faster to query 
 
Simple example to understand LINQ
 
C#
 
int[] numbers = { 0, 1, 2, 3, 4};
var a = from x in numbers
          where x > 2
          select x;
 
foreach (int i in a) {
    Console.WriteLine(i);
}
 
VB.NET
 
Dim numbers() As Integer = {0, 1, 2, 3, 4}
Dim a = From x In numbers
        Where x > 2
        Select x
 
For Each i As Integer In a
    Console.WriteLine(i)
Next
 
You can also use any SQL operator in LINQ
 
C#
 
int[] numbers = { 3, 5, 2, 6, 4 };
int count = numbers.Count();
Console.WriteLine(count);
 
VB.NET
 
Dim numbers() As Integer = {0, 1, 2, 3, 4}
Dim count As Integer = numbers.Count()
Console.WriteLine(count)

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to upload multiple files in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net