Thursday, November 30, 2017

LINQ: Language Integrated Query

LINQ is an acronym for Language Integrated Query. The Language Integrated part means that it is part of programming language syntax, and Query means, as it explains - is a means to retrieve data from a data source.

To retrieve data from different/multiple data sources in .NET, the underlying query languages use are:

  • SQL and ADO.Net for relational databases
  • XQuery and XSLT for XML

LINQ simplifies this working model by providing a common platform to execute the query and get the results. Basically LINQ query always works with objects, so the basic coding to query and transform data in XML, SQL database, ADO.NET Datasets, Collections and any other format for which LINQ provider available is common.

LINQ Archiecture

Below are different Types of LINQ:

  • LINQ to Objects
  • LINQ to XML (XLINQ)
  • LINQ to Dataset
  • LINQ to SQL (DLINQ)
  • LINQ to Entites

LINQ Syntax: Following are the two ways of LINQ syntaxes:

  • Lamda (Method) Syntax:
  • var testWords = words.where(w => w.length >10);
  • Query (Comprehension) Syntax:
  • var testWords = (from word in words where w.length > 10);

Per MSDN, Query expression consists below clauses:

  • FROM refers to Data Source
  • WHERE takes care of Filtering the requirement
  • SELECT takes responsibility of elements to return

Below are the Advantages and Disadvantages of using LINQ:

Advantages:
  • LINQ can be used against different data sources and it is not limited to RDBMS
  • Viewing table relationships is easy due to its hierarchical feature, which also enables composing queries by joining multiple table in less time
  • LINQ allows a single syntax while querying different data sources with support of its unitive foundation
  • LINQ is extensible, mean it is possible to use LINQ to query new data sources
  • A single LINQ query can join several data sources
  • Easy transformation for conversion of one data type to another, ex: SQL data to XML data
  • Debugging is easy due to its integration in C#
  • Writing more accurate queries is easy using LINQ intellisense
  • LINQ queries are typesafe as the errors will be type checked at compile time
Disadvantages:
  • For every query change, assembly needs to be recompiled and deployed
  • LINQ queries are not precompiled so need to take extra cautions to handle performance
  • In given scenarios, it is hard to understand advance LINQ queries
  • There will always be some things you can do in SQL but not in LINQ
With this I am concluding the illustration. Feel free to share your feedback.

Happy Programming !!!

No comments:

Post a Comment