LINQ makes it possible to access different data source with the same syntax.
It is a part of Microsoft .NET Framework 3.5 component.
It adds native data querying capabilities to .NET language using a syntax reminiscent of SQL.
Allows developers to query data structures using SQL-like syntax from within their applications code.
LINQ is another tool for embedding SQL queries into code.
The key feature of LINQ is its integration with widely used programming language, mad possible by the usage of syntax common to all kind of content.
LINQ provides consistent model for working with data across various kinds of data sources and formats.
In a LINQ query, you are always working with objects.
You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET Collections, and any other format for which a LINQ provider is available.
LINQ is a unified programming model for any kind of data. It can allow the developers to query and manage sequence of items ( Eg: In memory collections, tables in database, XML Nodes , and so on ).
LINQ enable you to query and manipulate data with a consistent model that is independent from data source (May be SQL Server, XML file, Arrays or Lists, Text file, e-mail message, SOAP message, etc., ).
SQL Like syntax used in LINQ is called “ query expressions ”.
LINQ queries are expressed in the programming language itself, and not as string literals embedded in the application code. So it offers several advantages:
Eliminates the need of using separate language.
With Visual Studio 2008 IDE you will also benefit compile time checking, static typing and Intellisense support.
LINQ to ADO.NET will implements the functionality needs to manipulate the relational data. It has the following modules:
LINQ to SQL, LINQ to DataSet and LINQ to Entity
The LINQ family of technologies provides a consistent query experience for
objects ( LINQ ),
relational databases ( LINQ to SQL )
XML ( LINQ to XML ).
All LINQ query operations consists of three distinct operations:
With these operators you will get a partial result. With Take , you have to specify the number of elements to take from the collection, whereas the Skip ignores the specified number of elements and takes remaining.
The TakeWhile/SkipWhile takes/skips the elements as long as a condition is true.
Standard Query Operators in LINQ
Set Operators: Distinct, Union, Intersect, Except
All these operators returns a collection set. The Distinct operator removes duplications from a collection. The Union operator returns unique elements that appear in the given two collections. The Intersect return elements that appear in both collections. The Except returns elements that appear in just one collection.
Element Operators : First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, ElementAt, ElementAtDefault .
All these operators just returns one element.
The First operator returns first element that satisfies the condition.
The FirstOrDefault is similar to First , but it returns a default value of the type if the element is not found.
Similarly, Last returns the last element that satisfies the condition.
With ElementAt operator you can query for specific position element to return.
The Single returns the only one element that satisfies the condition. If more then one element satisfies the condition an exception is thrown.
These operators converts the collections to an Array, IEnumerable, IList , etc.,
Generation Operators: Empty, Range, Repeat
These operators returns a new collection. With the Empty operator the empty collection is returned. The Range operator returns sequence of numbers and Repeat returns a collection with one repeated value.
Miscellaneous Operators: Concat, EqualAll
Custom Sequence Operators: Combine
Query Execution: Deferred, Immediate, QueryReuse
In Visual Studio 2008 includes LINQ provider assemblies that enable the use of LINQ with
.NET Framework Collections
SQL Server Databases
LINQ can be extended to support potentially any kind of data store. LINQ provider are being written by third parties for many web services and other database implementations.
Microsoft’s LINQ extensions are:
LINQ to SQL
LINQ to DataSet
LINQ to Entities
LINQ to XML
You can create any project on .NET Framework version 3.5 supports LINQ.
To enable basic LINQ functionality in your application :
Just reference to System.Core.dll.
Add using directive statement for System.Linq to your source code file or project.
To enable advanced LINQ functionality such as expression trees you need add using directive statement for System.Linq.Expressions .
To enable LINQ to XML in your file additionally add using directive for System.Xml.Linq .
To enable LINQ to SQL in your file
Add reference to System.Data.Linq.dll & System.Data.dll
Add the System.Data.Linq namespace reference with using directive.
To enable LINQ to DataSet in your file additionally
Add reference to System.Data.DataSetExtensions.dll & System.Data.dll
Add the System.Data.Linq namespace reference.
Visual Studio IDE Support for LINQ.
The Visual Studio 2008 IDE provides the following features that support LINQ application development:
Object Relational Designer :
It is a visual design tool that you can use in LINQ to SQL applications to generate classes in C# or VB that represent the relational data in an underlying database.
SQLMetal Command Line Tool:
It is a command-line tool that can be used in build processes to generate classes from existing databases for use in LINQ to SQL applications
LINQ-Aware Code Editors :
Both the C# and Visual Basic code editors support LINQ extensively with new IntelliSense and formatting capabilities.
The Visual Studio debugger supports debugging of query expressions.