Chitika

Monday, June 25, 2012

LINQ in c#

Language Integrated Query in C# :

As the Microsoft .NET platform, and its supporting languages C# and VB, have matured, it has become apparent that one of the more troublesome areas still remaining for developers is that of accessing data from different data sources. In particular, database access and XML manipulation is often cumbersome at best, and problematic in the worst cases.
The database problems are numerous.
 First, there is the issue that we cannot programmatically interact with a database at the native language level. This means syntax errors often go undetected until runtime. Incorrectly referenced database fields are not detected either. This can be disastrous, especially if this occurs during the execution of error-handling code. Nothing is more frustrating than having an entire error-handling mechanism fail because of syntactically invalid code that has never been tested. Sometimes this is unavoidable due to unanticipated error behavior.  Having database code that is not validated at compile time can certainly lead to this problem.
A second problem is the nuisance caused by the differing data types utilized by a particular data domain, such as database or XML data types versus the native language in which the program is written. In particular, dates and times can be quite a hassle.
XML parsing, iterating, and manipulation can be quite tedious. Often an XML fragment is all 
that is desired, but due to the W3C DOM XML API, an XmlDocument must be created just to perform various operations on the XML fragment.
Rather than just add more classes and methods to address these deficiencies in a piecemeal
fashion, the development team at Microsoft decided to go one step further by abstracting the fundamentals of data query from these particular data domains. The result was LINQ. LINQ is Microsoft’s  technology to provide a  language-level support mechanism for querying data of all types.
These types include in-memory arrays and collections, databases, XML documents, and more.



LINQ Is About Data Queries :
For the most part, LINQ is all about queries, whether they are queries returning a set of matching objects, a single object, or a subset of fields from an object or set of objects. In LINQ, this returned set of objects is called a sequence. Most LINQ sequences are of type IEnumerable<T>, where T is the  data type of the objects stored in the sequence. For example, if you have a sequence of integers, they would be stored in a variable of type IEnumerable<int>. You will see that IEnumerable<T> runs rampant in LINQ. Many of the LINQ methods return an IEnumerable<T>. In the previous examples, all of the queries actually return an IEnumerable<T> or a type that inherits from IEnumerable<T>. However, I use the var keyword for the sake of simplicity at this point, which is a new shorthand technique that I cover in Chapter 2. You will see that the examples will begin demonstrating that sequences are truly stored in variables implementing the IEnumerable<T>interface.


Components :
Because LINQ is so powerful, you should expect to see a lot of systems and products become LINQ compatible. Virtually any data store would make a good candidate for supporting LINQ queries. This includes databases, Microsoft’s Active Directory, the registry, the file system, an Excel file, and so on. Microsoft has already identified the components in this section as necessary for LINQ. There is  no doubt that there will be more to come. 


LINQ to Objects
LINQ to Objects is the name given to the IEnumerable<T> API for the Standard Query Operators. It is LINQ to Objects that allows you to perform queries against arrays and in-memory data collections.
Standard Query Operators are the static methods of the static System.Linq.Enumerable class that you use to create LINQ to Objects queries.


LINQ to XML
LINQ to XML is the name given to the LINQ API dedicated to working with XML. This interface was previously known as XLinq in older prereleases of LINQ. Not only has Microsoft added the necessary XML libraries to work with LINQ, it has addressed other deficiencies in the standard XML DOM, thereby making it easier than ever to work with XML. Gone are the days of having to create an XmlDocument just to work with a small piece of XML. To take advantage of LINQ to XML, you must have a reference to the System.Xml.Linq.dll assembly in your project and have a using directive such as the following:
using System.Xml.Linq;

LINQ to DataSet
LINQ to DataSet is the name given to the LINQ API for DataSets. Many developers have a lot of existing code relying on DataSets. Those who do will not be left behind, nor will they need to rewrite their code to take advantage of the power of LINQ.


LINQ to SQL
LINQ to SQL is the name given to the IQueryable<T> API that allows LINQ queries to work with
Microsoft’s SQL Server database. This interface was previously known as DLinq in older prereleases of LINQ. To take advantage of LINQ to SQL, you must have a reference to the System.Data.Linq.dll assembly in your project and have a using directive such as the following:
using System.Data.Linq;


LINQ to Entities
LINQ to Entities is an alternative LINQ API that is used to interface with a database. It decouples the entity object model from the physical database by injecting a logical mapping between the two. With this decoupling comes increased power and flexibility, as well as complexity. Because LINQ to Entities appears to be outside the core LINQ framework, it is not covered in this book. However, if you find that you need more flexibility than LINQ to SQL permits, it would be worth considering as an alternative.
Specifically, if you need looser coupling between your entity object model and database, entity objects comprised of data coming from multiple tables, or more flexibility in modeling your  entity objects, LINQ to
Entities may be your answer.



To get up-to-date information on LINQ, visit http://msdn.microsoft.com/en-us/library/bb397933.aspx






No comments:

Post a Comment