Tuesday, April 21, 2009

Language Integrated Query Part - I

LINQ

LANGUAGE-INTEGRATED QUERY is the name of technologies based on the integration of query capabilities in .NET compliant languages.

With LINQ, a query is now a language construct. By using query syntax, you can perform complex filtering, ordering, and grouping operation of data sources with minimum code. You use the same basic query expression patterns to query and transform data in SQL databases, ADO.NET Datasets, XML documents and streams, and .NET collections.

LINQ Parts

There are three parts of Query operation

  1. The Data source
  2. The Query (Query Expression)
  3. Execute the Query

The Data Source

Types that support IEnumerable(T) or a derived interface such as the generic IQueryable(T) are called Queryable Types. A Queryable Types requires no modification or special treatment to serve as a LINQ data source. If the source data is not already in memory as a queryable type, the LINQ provider must represent it as such.

LINQ provider

A library that implements the functionality of the standard query operators for a specific kind of data source. A LINQ provider translates queries expressed in Visual Basic or C# into the native language of the data source.

LINQ to ADO.NET

A technology that enables you to query over any enumerable object in ADO.NET by using the LINQ programming model. LINQ to ADO.NET consists of two related LINQ technologies: LINQ to DataSet and LINQ to SQL.

LINQ to DataSet

A LINQ technology that makes it easier and faster to query over data cached in a DataSet object. The queries are expressed in the programming language itself and not as string literals embedded in the application code.

LINQ to Objects

The use of LINQ to query in-memory data such as arrays and lists.

LINQ to SQL

A LINQ technology that provides a run-time infrastructure for managing relational data as objects. In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer.


 

LINQ to XML

An in-memory programming interface that enables you to work with XML from within the .NET Framework programming languages. You can query and modify a document, and then, after modifying it, you can save it to a file or serialize the result and send it over the Internet.

The Query

The query specifies what information to retrieve from the data source or sources. Optionally, a query also specifies how that information should be sorted, grouped, and shaped before it is returned. A query is stored in a query variable and initialized with a query expression.

Query Operators

The standard query operators are the methods that form the Language-Integrated Query (LINQ) pattern. Most of these methods operate on sequences, where a sequence is an object whose type implements the IEnumerable<(Of <(T>)>) interface or the IQueryable<(Of <(T>)>) interface. The standard query operators provide query capabilities including filtering, projection, aggregation, sorting and more.

Two Sets of LINQ Operator

There are two sets of LINQ standard query operators:

  1. One that operates on objects of type IEnumerable<(Of <(T>)>).
  2. The other that operates on objects of type IQueryable<(Of <(T>)>).

The methods that make up each set are static members of the Enumerable and Queryable classes, respectively. They are defined as extension methods of the type that they operate on. This means that they can be called by using either static method syntax or instance method syntax.

The standard query operators differ in the timing of their execution, depending on whether they return a singleton value or a sequence of values. Those methods that return a singleton value (for example, Average and Sum) execute immediately. Methods that return a sequence defer the query execution and return an enumerable object.

Query Expression

  1. Query expressions can be used to query and to transform data from any LINQ-enabled data source.
  2. Query expressions are easy to learn because they use many familiar C# language constructs.
  3. The variables in a query expression are all strongly typed, although in many cases you do not have to provide the type explicitly because the compiler can infer it.
  4. A query is not executed until you iterate over the query variable in a foreach statement.
  5. At compile time, query expressions are converted to Standard Query Operator method calls according to the rules set forth in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax. However, in most cases query syntax is more readable and concise.
  6. As a rule when you write LINQ queries, use query syntax whenever possible and method syntax whenever necessary. There is no semantic or performance difference between the two different forms. Query expressions are often more readable than equivalent expressions written in method syntax.
  7. Some query operations, such as Count or Max, have no equivalent query expression clause and must therefore be expressed as a method call. Method syntax can be combined with query syntax in various ways.
  8. Query expressions can be compiled to expression trees or to delegates, depending on the type that the query is applied to. IEnumerable<(Of <(T>)>) queries are compiled to delegates. IQueryable and IQueryable<(Of <(T>)>) queries are compiled to expression trees.

Iteration Statement: foreach

The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the information that you want, but should not be used to change the contents of the collection to avoid unpredictable side effects.

At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword. A foreach loop can also be exited by the goto, return, or throw statements.

Snippet


 

// Traverse a one-dim array

int []a={11,22,33};

foreach(int p in a) {

Console.WriteLine(p);

}


 

// Traverse a two-dim array

int [,]a={{11,22,33},{22,33,44},{66,77,88}};

foreach (int p in a){

Console.WriteLine(p);

}


 

// Generic List

List<float> p = new List<float>();

p.Add(100f);

p.Add(100.23f);

p.Add(56.44f);

foreach (float t in p){

Console.WriteLine(t);

}


 

// Generic Set

HashSet<int> p = new HashSet<int>();

p.Add(11);

p.Add(40);

p.Add(3);

foreach (int t in p){

Console.WriteLine(t);

}


 

// Generic Stack

Stack<int> p = new Stack<int>();

p.Push(33);

p.Push(44);

p.Push(55);


 

foreach (int t in p){

Console.WriteLine(t);

}


 

// Traverse an ArrayList

ArrayList a = new ArrayList();

a.Add(100);

a.Add("Hello");

a.Add(10.44d);

a.Add(34.44f);

a.Add(DateTime.Now);


 

foreach (object p in a){

Console.WriteLine(p + " " + p.GetType().Name );

}


 

// Traverse Dictionary Object – Hashtable

Hashtable a = new Hashtable();

a.Add("no", 100);

a.Add("name", "Mr. Rajesh");

a.Add("address", "Mehsana");


 

foreach (object p in a){

DictionaryEntry e = (DictionaryEntry)p;

Console.WriteLine(e.Key + " " + e.Value);

}


 


 


 


 

No comments: