Tuesday, June 3, 2014

LINQ

LINQ is an acronym for Language Integrated Query, which is descriptive for where it's used and what it does. The Language Integrated part means that LINQ is part of programming language syntax, Query, explains what LINQ does; LINQ is used for querying data. The generic term "data" indicates LINQ can be used to query many different types of data, including relational, XML, List objects, etc. 
Lambda Expression: Linq is always associated with Lambda Expressions. In .NET 2.0 we have the concept of Anonymous Methods that allows you to write function body inline without the need of writing a delegate function. Lambda Expression of .NET 3.5 is to consider the concept of Anonymous function writing. 

Ex: Normal way to write a function:
int i = this.compute(10);
private int compute(int value) 

   return (value + 2);
}

We can replace the above code with inline anonymous function like this:
delegate int DelType(int i);
DelType dd =  delegate(int value)
              {
               return (value +2);
              };
int i = dd(10);

Lambda expressions allow predicates and other projection functions to be written in-line with a concise syntax, and support full lexical closure. They are captured into parameters as delegates or expression trees depending on the Query Provider. Usage of delegate and lamda expressions:
delegate int DelType(int i, int j);
DelType d = (value1,value2) => value1 + value2;
int i = d(10,20) // Returns 30

Types of LINQ

LINQ to Objects: Provides queries on any kind of C# in-memory object, such as arrays, lists, and other collection types.

LINQ to XML: Provides creation and manipulation of XML documents using the same syntax and general query mechanism as the other LINQ varieties.

LINQ to ADO.NET: ADO.NET or Active Data Objects for .NET is an umbrella term that includes all the different classes and libraries in .NET for accessing data in databases, such as Microsoft SQL Server, Oracle, and others. LINQ to ADO.NET includes LINQ to Entities, LINQ to DataSet, and LINQ to SQL.

LINQ to Entities: The ADO.NET Entity Framework is the newest set of data interface classes in .NET 4, recommended by Microsoft for new development.

LINQ to DataSet: The DataSet object was introduced in the first version of the .NET Framework. This variety of LINQ enables legacy .NET data to be queried easily with LINQ.

LINQ to SQL: This is an alternate LINQ interface for .NET 3.5, targeted mainly at Microsoft SQL Server, that has been superseded by LINQ to Entities in .NET 4.

PLINQ: PLINQ or Parallel LINQ extends LINQ to Objects with a parallel programming library that can split up a query to execute simultaneously on a multicore processor.

Most commonly used ones are: 
1. LINQ (Linq to Objects)
2. DLINQ (Linq to SQL)  -- will not explain about this.
3. XLINQ (Linq to XML)

1. Linq to objects: The below Linq will give comma separated values of Param1

EX 1:
Class SomeClass
{
    Public string Param1;
    Public string Param2;
}

List<SomeClass> ltSomeClass = new List<SomeClass>();
//Add some objects of SomeClass

String Param1Values = string.Join(",", ltSomeClass.Select(x => x.Param1).ToArray());

3. Linq to Xml:

EX: Gets the value from xml based on attribute name

Xml Sample: <Root><Child1><Child2 attribute_name1 = "abc"></Child2></Child1></Root>

var Param1 = (from f in xDoc.Descendants("RootNode").Descendants("Child1")                                               from a in f.Elements("Child2")
                    where a.Attribute("name").Value == "attribute_name1"
                    select a.Value).ToArray();



NOTE: I dont take the ownership of the content,  I give credits to the following links:
http://en.wikipedia.org/wiki/Language_Integrated_Query
http://www.codeproject.com/Articles/19154/Understanding-LINQ-C
http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
http://w3mentor.com/learn/asp-dot-net-c-sharp/c-asp-net-linq/types-of-linq/

No comments:

Post a Comment