Saturday 22 February 2014

LINQ Introduction

Introduction
  • LINQ is a new technology introduced in .NET 3.5
  • LINQ is acronym for Language Integrated Query
  • The LINQ feature is one of the major differences between the .NET 3.0 and .NET 3.5 frameworks
  • LINQ is designed to fill the gap that exists between traditional .NET, that offers strong typing, and a full Object-Oriented approach
LINQ introduces an easy-learning approach for querying and modifying data and can support querying various types of data sources including relational data, XML and in-memory data structures.

Advantages of Using LINQ
LINQ offers the following advantages:
  1. LINQ offers a common syntax for querying any type of data sources
  2. Secondly, it binds the gap between relational and object-oriented approachs
  3. LINQ expedites development time by catching errors at compile time and includes IntelliSense & Debugging support
  4. LINQ expressions are Strongly Typed.

What we mean by Strongly Typed?
Strongly typed expressions ensure access to values as the correct type at compile time & prevents type mismatch errors being caught when the code is compiled rather at run-time.
Core Assemblies in LINQ
The core assemblies in LINQ are:
  1. using System.Linq
    Provides Classes & Interface to support LINQ Queries
  2. using System.Collections.Generic
    Allows the user to create Strongly Typed collections that provide type safety and performance (LINQ to Objects)
  3. using System.Data.Linq
    Provides the functionality to access relational databases (LINQ to SQL)
  4.  using System.Xml.Linq
     Provides the functionality for accessing XML documents using LINQ (LINQ to XML)
  5. using System.Data.Linq.Mapping
    Designates a class as an entity associated with a database.
Types of LINQ
LINQ provides three basic types of queries, each type offers specific functionality and is designed to query a specific source. The three basic types of queries are:
  1.  LINQ to Objects
  2. LINQ to XML(or XLINQ)
  3. LINQ to SQL(or DLINQ)
Let us briefly discuss each of them.
LINQ to Objects
  • LINQ to Object is the basics of LINQ
  • It enables us to perform complex query operations against any enumerable object (object that supports the IEnumerable interface)
LINQ queries provide the following advantages over traditional foreach loops:
  1. They are more concise & readable, especially when filtering multiple conditions
  2. Provides powerful filtering, grouping and ordering with little coding