Friday, October 24, 2008

What is LINQ

LINQ (Language Integrated Query) is a component released within the .NET 3.5 Framework. It is one of the most powerful features of .NET 3.5. It serves the purpose of querying objects. It is a Microsoft programming model and methodology that essentially adds formal query capabilities into Microsoft .NET-based programming languages. LINQ offers a compact, expressive, and intelligible syntax for manipulating data. The real value of LINQ comes from its ability to apply the same query to an SQL database, a DataSet, an array of objects in memory and to many other types of data as well.
There is no one definition for LINQ, but it aims to solve the problem where we currently use different techniques for manipulating and selecting data from databases versus XML versus object collections; LINQ aims to make our life easier by giving us a coordinated, consistent and efficient syntax from our development environment and by using your one chosen programming language, rather than switching between programming languages.The current LINQ family of technologies and concepts allows an extensible set of operators that work over objects, SQL data and XML data sources. The generalized architecture of the technology also allows the LINQ concepts to be expanded to almost any data domain or technology, so what LINQ is today will expand in the future.

LINQ to SQL
The LINQ to SQL provider is just one part of the overall framework, however, it is a very powerful and attractive option when and ORM (see Object-relational mapping below) is called for. LINQ uses an SQL-like syntax to make query expressions well beyond the capabilities of embedded SQL as implemented in programming languages. That's because embedded SQL uses a simplified, streamlined syntax to add SQL statements to other programming languages, where there's no attempt to integrate such statements into the native syntax and typing mechanisms. Thus, you can't invoke native language structures such as functions in embedded SQL statements, as you can using LINQ, because it is implemented to use native syntax, structures, and typing mechanisms. Furthermore, LINQ may be used to access all kinds of data, whereas embedded SQL is limited to addressing only databases that can handle SQL queries.

In short LINQ to SQL can operate as middle ware between business objects and the database, the advantage of this may seem abstract but consider the following example. In SVT if we want to get just a single standard and do some work on it we first have to create a new object of type standard, next we tell the new object that it’s id is say 21. From here the standard object has been programmed to take that id and go call on other objects from the data namespace, it call a specific one the passes the standard id to a stored procedure which returns a query containing the desired info. Next, that query is returned to the standard object and it essentially, loops over all the columns in the query and populates a property in the new standard object for each one. Now (very simplistically speaking) we have our standard object, it has the methods and properties needed to do work. After we change a few things and tell the object to update the database, the whole process happens again in reverse. If on the other hand we were using LINQ we would simply write a 1 line SQL like expression passing the id of the standard we desired, and it would return a standard object ready to go. We could then edit that object and tell it to update and be done with it.

LINQ to Objects
The LINQ to Objects provider is used for querying in-memory collections, using the local query execution engine of LINQ. The code generated by this provider refer the implementations of the standard query operators as defined in the Sequence class and allows IEnumerable collections to be queried locally.

What this means is that when we want specific information from an array or other type of collection, instead of looping over the entire array, building another array of the desired data, we can simple query the array as if it were a table in a database.

Conclusion
The overall gain in productivity we realized by switching to LINQ is immeasurable, instead of taking all day to get everything needed in the SQLGateway class up and running, our team can do a LINQ query in seconds returning full blown already initialized objects. In an effort to make the implementation as effective as possible I dedicated months of personal research, read many books on the subject, countless articles, and attended several workshops. I felt that to make it all work I needed to become the departmental expert helping the others along as they were ready. As each team member came on board with the new concepts they too became very excited with the prospects and really wish other languages such as Coldfusion had similar features.

Object-relational mapping (aka ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" which can be used from within the programming language.

No comments:

Post a Comment