Wednesday, November 23, 2016

LINQ - Introduction

LINQ (Language Integrated Query) - It was introduced with .NET framework 3.5 & visual studio 2008. It bridges the gap between the worlds of objects and the words of data. Time to time various languages have been introduced to query (an expression which retrieves data from data source) data from various data sources like SQL for Relational Database, XQuery for XML and so on. Hence, developers had to learn a new query language for each type of data sources or data formats, that they will be willing to use in their application. LINQ simplifies this situation by offering consistent model for working with data across various kinds of data sources or formats. 


Architecture  -  LINQ has 3 layer architecture:

  1. Language extension for LINQ in .NET
  2. LINQ Providers - (Example: LINQ to Objects, LINQ to SQL, LINQ to XML etc)
  3. LINQ enabled Data Source - Objects implementing IEnumerable<T> or IQueryable<T> 
Query Operation  - All LINQ query consists of three distinct actions:

  1. Obtain the data source.
  2. Create the query.
  3. Execute the query

Example: querying an integer array to get the numbers which are divisible by 2. Here the query returns the IEnumerable<T> object.
  • The Data SourceThe basic rule: A LINQ data source is any object that supports the generic IEnumerable<T> interface, or an interface that inherits from it.
  • The Query - In LINQ, the query variable itself takes no action and returns no data. It just stores the information that is required to produce the results when the query is executed at some later point.
  • Query Execution
    • The query variable only stores the query commands, the actual execution of the query is deferred until you iterate over the query variable in a foreach statement. This concept is referred to as deferred execution (refer the above code for example). Since the query object doesn't hold the query results, you can execute it as often as you like
    • Queries that perform aggregation functions (Count, Max, Average, and First) over a range of source elements execute without an explicit foreach statement (the foreach is already been used internally to obtain the result). This is called the force immediate execution and these types of queries return a single value, not an IEnumerable collection. You can also force immediate execution to the query in above example by calling the ToList or ToArray, this also caches all the data in a single collection object.
Syntax - There are two basic ways to write LINQ quereis:


  • Query Syntax - Aka Comprehension Syntax or Query Expression
  • Method Syntax - Aka Method Extension Syntax or Fluent
LINQ Providers 
  • LINQ to Objects (Not a provider actually, it refres to the use of LINQ queries with any IEnumerable or IEumerable<T> collection directly) 
  • LINQ to XML (Formally known as XLINQ)
  • ADO.NET LINQ Technologies
    • LINQ to DataSet
    • LINQ to SQL (formally known as DLINQ)
    • LINQ to Entities
There are many third party LINQ providers are also available like LINQ to Parallel (PLINQ) , LINQ to Excel, and LINQ to JavaScript, etc. 


Advantages: 
  • Integrate
    • LINQ offers IntelliSense which means writing more accurate queries easily.
    • LINQ offers syntax highlighting that proves helpful to find out mistakes during design time.
    • Increases development time, as the syntax highlighting and IntelliSense support allow you to get more work done in less time.
    • LINQ code is shorter and cleaner than traditional techniques for querying data and, therefore, is much easier to maintain.
    • Debugging is easy due to its integration in the C# language.
  • Unitive 
    • Before LINQ, developers who queried data frequently needed to master multiple technologies. For example: 
      • SQL to query a database
      • XPath, Dom, XSLT, or XQuery to query and transform XML data
      • Web services to access some forms of remote data source
      • Looping and branching to query the collections in their own programs
      LINQ simplifies these tasks by providing a single, unified method for querying diverse types of data. Developers don’t have to master a new technology simply because they want to query a new data source. Now using LINQ, they can call on their knowledge of querying local collections when they query relational data, and vice versa.



    • The code becomes easier to maintain, because you are using the same syntax regardless of the type of data you query.
  • Extensible - It is possible to use knowledge of LINQ to querying new data source types.
  • Declarative
    • Because LINQ is declarative, it usually allows you to write concise code that is easy to understand and maintain.
    • It allows developers to simply state what they want to do without worrying about how it is done.
  • Hierarchical - Viewing relationship between two tables is easy with LINQ due to its hierarchical feature and this enables composing queries joining multiple tables in less time.
  • Composable
    • LINQ offers the facility of joining several data sources in a single query or in a series of related query. 
    • It's easy to break complex problems into a set of short comprehensible queries that are easy to debug.
  • Transformative - LINQ make it easy to convert data of one type into a second type. For instance, you can easily transform SQL data into XML data using LINQ.

No comments:

Post a Comment