Oct 30, 2009

What is LINQ

The motivation behind LINQ was to address the conceptual and technical difficulties encountered when using databases with .NET programming languages. LINQ means Language INtegrated Query. Microsoft intention was to provide a solution for the problem of object relational mapping, and simplify the interaction between objects and data sources. LINQ glues several worlds together.

LINQ to XML enables an element-centric approach in comparison to the DOM approach. Two classed that the .NET Framework offers are XmlReader and XmlWriter. Expressing queries against XML docments feels more natural than having to write a lot of code with several loop instructions.

Using LINQ to XML:

using System;
using System.Linq;
using System.XML;
using System.XML.Ling;


class Book
{
public string Publisher;
public string Title;
public int Year;

public Book( string title, string publisher, int year)
{

Title = title;
Publisher = publisher;
year = year;
}
}

static class LinqToXML
{
Book [] books = new Book[] {
new Book ("Javascript the Missing Manual", 2008),
new Book (" Python Essential Reference", 2009),
new Book (" Head First Java", 2005),
};

XElement xml = new XElement ("books", from book in books
where book.Year == 2008
select new XElement ("book",
new XAttribute ("title", book.Title),
new XElement ("publisher", book.Publisher)));
console.WriteLine(XML);

}
}

As you can see there aren't any for loops.
http://msdn.microsoft.com/en-us/netframework/aa904594.aspx

1 comment:

  1. Dane,
    thank you for this post! I had some software development projects. These projects were realized in Java so we used Hibernate for the object relational-mapping. Hibernate featured dynamically created SQL queries. Another common way for ORM in java is JPA. We tried it in combination with Spring and we liked it.

    ReplyDelete