LINQ « Datadial Blog
0208 6000 500

Alex

July 14th, 2008.


Filed under Development

LINQ

The .Net framework 3.5 has introduced a new database query language known as LINQ (pronounced ‘LINK’). LINQ stands for Language Integrated Query and has been developed to make querying data a first class programming concept. LINQ can be used with any data source and can be expressed in any .Net language. It provides full type-safety, compile time checking, step through debugging, refactoring and (when incorporated with Visual Studio 2008,) full intellisense.

LINQ allows you to model a relational databases using object relational mapping. You can then run queries and perform inserts, updates and deletes using its object programming syntax.

LINQ is not VB .Net or C#, even though it can be expressed using a similar syntax. It contains standard query operators that allow you to work with data in a more intuitive way regardless of whether you are working with a relational database, XML or in-memory collections (i.e. generic lists).

Some of the main query operators are:

  • Select
  • OrderBy
  • Where
  • SelectAll
  • TakeWhile
  • Take
  • Skip
  • First
  • SkipWhile

 

These are similar to their SQL equivalents while some operators add additional functionality.

LINQ fully supports views, transactions and stored procedures and with the addition of the ‘LINQ-to-SQL’ designer users can easily create object representations of any SQL Server database. The LINQ-to-SQL designer is a type of wizard that can automatically create the mapping classes needed to link an object to a database table or view. This saves developers a huge amount of time as it dynamically maps table column names to a mapping class and creates the necessary properties within that class.

The code generated by the wizard is split into several classes depending on how many tables that have been targeted. The main class (which is of type System.Data.Linq.DataContext) relates to the database as a whole, and the remaining classes relate to each of the tables or views.

Using the LINQ-to-SQL wizard isn’t essential as these classes can be created manually. A happy medium would be to allow the wizard to generate the necessary code for you then customise and strip out any unnecessary content.

Data Modelling
Many developers may find the way LINQ creates object relational mapping very familiar to how the currently work. It models one-to-one class entity relationships with a key (ID) property (e.g. a product has a category, therefore a category ID is placed within the product class). In the case of a one-to-many relationship (e.g. an order object has many order-lines); a collection of order-lines are placed within the order object.

Conclusion

Many developers are used to the tedious but essential process of mapping relational data to .Net objects but with the introduction of LINQ this process can be made a lot smoother as it provides a bridge between the two domains. Additionally you can use some of the other LINQ data providers to map data like: LINQ-to-XML, LINQ-to-Entities, LINQ-to-Objects, LINQ-to-SharePoint and LINQ-to-Terra Server. The addition of LINQ to the latest version of the .Net framework makes writing error prone mapping objects and stored procedures a thing of the past.

Leave a comment

Recent Posts »

Our work »

What we do »

Who we work with »

Got Questions? Lets Talk »