Wednesday, May 29, 2013

LINQ 01- Introduction

Language INtegrated Query, is a simplified syntax for processing collection of objects.

We can say most of the new language features in .NET 3.5 and above are innovated to realize, simplify and improve LINQ syntax.

Basically LINQ is built around IEnumerable interface. Almost all of the .Net collection objects from Arrays to Generic List and Dictionary, are implemented with IEnumerable interface.

LINQ syntaxes are realized with Extension methods and Delegates. For eg, LINQ to Objects are built with Extension methods for IEnumerable with delegate parameters.

To simplify the usage of delegate parameters, anonymous methods, lambda methods and lambda expressions are introduced.

A simple LINQ Query

var s = new string[] { "Apple", "Orange", "Mango" };
var l = from i in s
select i.Length;

Above is a simple LINQ query, just selects the length of string items from an array. Above can be achieved without LINQ as

var s = new string[] { "Apple", "Orange", "Mango" };
var l = s.Select(i => i.Length);

Following concepts can be used to simply the LINQ statements

  • Extension Methods
  • Lambda Expressions
  • Anonymous Types
  • Automatic Type Inference
  • Auto Implemented Properties
  • Object and Collection Initializers

No comments:

Post a Comment