I had the great pleasure of helping to represent one of Microsoft's new technologies at the PDC this year, its called LINQ; Language INtegrated Query. I hope to provide a number of posts about the details of LINQ and XLinq over the next few weeks, but I thought I should start with a simple introduction first, for everyone who was not able to make it to this event.
LINQ is a collection of new language features (which are planned to be included in future versions of C# and VB.Net) and new framework APIs that allow you to query and transform "data" from directly within the language. There are two interesting things to note about this sentence:
In other words, LINQ is a syntax for querying data directly in the language , but it does not specify WHAT KIND of data is being queried (this statement is slightly inaccurate, because it is not technically a 'syntax' but rather a collection of language features which are implemented with different syntax for different languages, but we can gloss over that for the moment). Anyone can choose to implement their APIs in a way that allows LINQ language features to query that data. Now you may be thinking that this is already true with today's technology. After all, if you implement an OLE DB provider for your data, then you can query it using SQL. There are (at least) three major issues with this:
To put it another way:
Ok, so LINQ is all about enhancing developer productivity when working with data. So what does a LINQ query look like? The following simple example shows us one way to query some xml data using LINQ and the new LINQ-Enabled XML API:
IEnumerable<XElement> transactions = from transaction in myXmlElement.Element("AllTransactions").Elements("Transaction") where (double)transaction.Attribute("amount") > 1000.0 select transaction;
AllTransactions/Transaction[@amount>1000.00] Now if you already understand XPath, then you are probably thinking 'Why would I move to a verbose syntax like LINQ when the XPath is so much more compressed?' Which is a fair statement, but it misses the point. The point is that I can use the same syntax to query my database: IEnumerable<Individual> individuals = from individual in myDatabase.Individuals where individual.Role=="Supreme Commander" select individual; And you can use the same syntax to query collections of in-memory objects: IEnumerable<char> firstInitials = from name in myNameList where name.Length<0 select name[0]; Or you can do a join between xml and your database (sorry for the complicated example here): IEnumerable<XElement> transactions = from individual in myDatabase.Individuals, transaction in report.Element("AllTransactions).Elements("Transaction") where individual.Name == (string)transaction.Attribute("name") && individual.Role=="Supreme Commander" && (double)transaction.Attribute("amount") > 1000.0 select transaction; The point is, LINQ is a general purpose language for querying and transforming data, so of course there will be cases where a domain-specific query language (like xslt) will be more powerful, more compact, or more expressive. Our hope though, is that for those common cases where you don't need the extra power, this will be (as one PDC-goer put it) 'the last weird query language you'll ever have to learn.'
AllTransactions/Transaction[@amount>1000.00]
Now if you already understand XPath, then you are probably thinking 'Why would I move to a verbose syntax like LINQ when the XPath is so much more compressed?' Which is a fair statement, but it misses the point. The point is that I can use the same syntax to query my database: IEnumerable<Individual> individuals = from individual in myDatabase.Individuals where individual.Role=="Supreme Commander" select individual; And you can use the same syntax to query collections of in-memory objects: IEnumerable<char> firstInitials = from name in myNameList where name.Length<0 select name[0]; Or you can do a join between xml and your database (sorry for the complicated example here): IEnumerable<XElement> transactions = from individual in myDatabase.Individuals, transaction in report.Element("AllTransactions).Elements("Transaction") where individual.Name == (string)transaction.Attribute("name") && individual.Role=="Supreme Commander" && (double)transaction.Attribute("amount") > 1000.0 select transaction; The point is, LINQ is a general purpose language for querying and transforming data, so of course there will be cases where a domain-specific query language (like xslt) will be more powerful, more compact, or more expressive. Our hope though, is that for those common cases where you don't need the extra power, this will be (as one PDC-goer put it) 'the last weird query language you'll ever have to learn.'
Now if you already understand XPath, then you are probably thinking 'Why would I move to a verbose syntax like LINQ when the XPath is so much more compressed?' Which is a fair statement, but it misses the point. The point is that I can use the same syntax to query my database:
IEnumerable<Individual> individuals = from individual in myDatabase.Individuals where individual.Role=="Supreme Commander" select individual;
And you can use the same syntax to query collections of in-memory objects:
IEnumerable<char> firstInitials = from name in myNameList where name.Length<0 select name[0];
Or you can do a join between xml and your database (sorry for the complicated example here):
IEnumerable<XElement> transactions = from individual in myDatabase.Individuals, transaction in report.Element("AllTransactions).Elements("Transaction") where individual.Name == (string)transaction.Attribute("name") && individual.Role=="Supreme Commander" && (double)transaction.Attribute("amount") > 1000.0 select transaction; The point is, LINQ is a general purpose language for querying and transforming data, so of course there will be cases where a domain-specific query language (like xslt) will be more powerful, more compact, or more expressive. Our hope though, is that for those common cases where you don't need the extra power, this will be (as one PDC-goer put it) 'the last weird query language you'll ever have to learn.'