According to Microsoft LINQ is “a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities”. What they actually mean is that its a very interesting way of doing Functional programming using C#. I make the BIG distinction here between LINQ and LINQ/SQL (i.e. LINQ for SQL databases) as the latter is probably not something that will remain in the Framework long term. It is probably going to be replaced with the ADO.NET Entity Framework.
As an introduction you should download LinqPad which is an excellent tool for the beginner or experience functional programmer. Not only does it provide a number of samples for a quick start, it also provides a test bed where you can run your queries directly against your data sources.
Installation Instructions
Download to your local machine the LinqPad.exe file from the website.
Double click the LinqPad icon and you should be presented with the application desktop.
Testing your queries
The easiest way to test your queries is to do it against a data source, so click the “Add Connection” on the left of the screen.
Use the options to connect to your database server and click OK.
Next you select the Database using the “Database” dropdown in the top right and then all you need to do is type your LINQ queries into the work area below.
Simple where example
from c in Companies
where c.Comp_name.Contains("Iona Tech")
select c
Pressing the play button will execute the query.
This example is against a SQL database but that is only for convenience there is no issue at all running queries against array or other collections and that is where the power of this technology can be seen.
Example of Parsing strings
from word in "The quick brown fox jumps over the lazy dog".Split()
orderby word.Length
select word
Example working with arrays
from n in new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }
where n < 5
select n
- or simply -
(new[] {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 } ).Where (n => n < 5)
LINQ has a future for the .Net Framework, you only need to look at the developments with PLINQ and the Entity Framework to know that the syntax will start to move into day-to-day C# code. Also I’d recommend having a review of the F# Development Centre, this is an extension for Visual Studio for developers who really, really, really like their functional programming.
Interesting examples
Here are some links to resources which will come in useful.
No comments:
Post a Comment