Tuesday, 15 April 2014

What is LINQ?

1. It is a name for a set of technologies based on the integration of query capabilities directly into the C# language
2. In LINQ you are always working with objects  in a form of queries.
3. With LINQ, same coding pattern is used for all data source.



What is Query?

A query is a set of instructions that describes 
  • what data to retrieve from a given data source (or sources) 
  • and what shape and organization the returned data should have.
Execution of LINQ involve three operations
  • Obtain Datasource
  • Create Query Expression
  • Execute Expression

I. Obtain Data Source

Earlier: The source data is organized logically as a sequence of elements of the same kind. 
  • A SQL database table contains a sequence of rows.
  • an ADO.NET DataTable contains a sequence of DataRow objects.
  • XML file, there is a "sequence" of XML elements (although these are organized hierarchically in a tree structure). 
  • Collection contains a sequence of objects.
WITh LINQ: the specific type and structure of the original source data is not important.  The application always sees the source data as an IEnumerable<T> or IQueryable<T> collection. 


II. Query Expression

  • Query Expression can be used to query and to transform data from any LINQ enabled data source.
  • A query expression must begin with a from clause and must end with a select or group clause.

II. Execute Expression
  • A query is not executed until you iterate over the query variable in foreach statement.

Basic Query Operations

Obtaing DataSource
From clause is used to include

  • Data source 
  • Range Variable : which store each element of data source.
  • Syntax:  from <rangevariable> in <datasource objcet>


Selection 
Select clause is used to specify type of result  sequence returned after execution of query
Syntax:
select <rangevariable>;
select new { rangevariable1, rangevariable2,..}


Filtering 

  • Only those element for which the expression is true is returned by the query.
  • Any C# logical and relation operators can be used to specify expression. 
  • Syntax: where <condition1 on range variable>  [<logical operator>] <condition2 on rangevariable>


Ordering

  • orderby clause cause the data to be returned in sorted order (ascending or descending)
  • Default order is ascending.
  • Syntax: orderby  rangevariable  [[ascending | descending], ..]


Executing

  • foreach loop is used to iterate through each element of range variable returned by LINQ query.
  • Syntax: foreach (datatype  loopvariable  in  linqqryvariable)  ….    }



Grouping

  • Groupby clause is used to group your results based on a key specified by the user
  • Grouping refers to the operation of putting data into groups so that the elements in each group share a common attribute. 
  • Syntax: group <range variable> by <grouping criteria>



Let clause
  • Let clause creates a new range variable and store the result of sub expression.
  • Once initialized range variable cannot store another value
  • Syntax: let <new range variable> = [LINQ expression | other expression]

Set Operations
Based on the presence or absence of equivalent elements within the same or separate collections. The operations are
  • Distinct: Remove duplicate values from the collections
  • Except: Elements in first input sequence that are not in second input sequence.
  • Intersect: Elements that are common to both input sequence.
  • Union: Unique elements from both sequence.

Basic Query Operations EXAMPLES

1. Selecting Data   
//data source
  int[] numbers = { 2, 4, 1, 3, 5, 34, 67, 89, 90, 50 };

lShow.Text += "<br><br><b> Numbers Are</b><br>";
//query expression
var qry1 = from n in numbers
                   select n;
//executing
foreach (var data in qry1)
{
     lShow.Text += "<br>" + data;
}

2. Filtering Data  
To display all nos between 10 and 50.
//data source
  int[] numbers = { 2, 4, 1, 3, 5, 34, 67, 89, 90, 50 };

 lwhere.Text+= "<br><br><b> Numbers Are</b><br>";
//query expression
var qry1 = from n in numbers
                   where n>10 && n<50
                   select n;
//executing
foreach (var data in qry1)
 {
     lwhere.Text += "<br>" + data;
}