Simple Experiment with LINQ
LINQ, Language Integrated Query, is a new feature in the version of 3.5. I haven’t had a chance to play with it before, so I thought making some 101 type simple experiments would be a great starting point.
When you create a new ASP.NET web project with VB.NET in VWD 2008, web.config file already comes with the required assemblies and namespaces.
Let’s have a simple array with some different country names in it, and try to find the country names starting with letter A within this array by using non-LINQ (which I call “traditional”) and LINQ ways.
In order to see the basic usage of LINQ, I will use the literal control.
First literal will show the results when CNames is looped through all country names and all these country names are checked whether they start with A or not, whereas second literal will show the result of its associated LINQ query.
Pay attention to the Order By part of the query which sorts the result easily.
Now, I will use a DropDownList control in order to see what happens if I would use the result of a LINQ query as a direct data source.
Like in the “literal” approach, first dropdown will show the results when CNames is looped through all country names and selected country names will added to an arraylist (which will be the data source of this first dropdown) in order to have a sorting capability. For the second dropdown, I will set the result of LINQ query as a direct data source to it.
The concept of “data source” is now having an evolution with LINQ. Depending on the application scenarios, I guess anything can be a direct data source for the data controls.
After writing all these codes, browser looks like:
I haven’t touched to LINQDataSource yet, but this post gives me the enough initial opinion that LINQ seems to be a great addition to the language. It makes more sense to me when I read the LINQ’ed code as a developer point of view, and I believe it would be easier to enhance or modify the application when its code is more readable. The only issue is I have been used to the syntagm of “Select From Where” instead of using From at the beginning and Select later. Anyway, I will get used to it.