This blog is about how to use a new feature in Visual Studio.Net 2008 called LinQ.
LinQ is a new way to fetch and query data for our .Net applications. LinQ is short for Language Integrated Query. LinQ gives us a new way to write our queries, along with our code, thus the term "Integrated".
LinQ can retrieve data from a SQL Database, an XML File or a Collection. In this demo, I will show how to use LinQ and its features and how to use it to retrieve data from a collection.
First, I am going to create a simple application that will show data from a collection of type Person (Person.cs).
The application will filter the data according to a date entered in a textbox, and bind the output to a grid. The data bound to the grid will be the people whose birthday is less than the date entered in the textbox.
You will also notice that our demo application has a combobox. I will be using this combobox to show you two things. First is how to use LinQ and bind the data to a combobox and the second is how, or more importantly, when, LinQ actually executes the query.
The Person class is a simple object with a Name and a Birthday property. This is all that we will be needing for this demo.
Then I will create a new List of type Person then call it People. Then I will populate the People collection using code.
Now I’m going to add a Procedure to fetch the data according to our need and place this in the Grid. Then I will call this procedure in button1’s click event.
This method is fairly straight simple and will probably look at how you are fetching your data right now. By running a foreach on your collection, then comparing the values to a given condition one by one and place the data in another collection called results. Then you use the results collection to populate the grid.
This method is OK but very imperative and if you don’t "speak" C#, this could be considered confusing and too many moving parts.
using LinQ, the code should look like this:
LinQ’s syntax was intentionally designed to look very much like SQL. This was done so that the language learning will not be very difficult specially for developers with previous experience with SQL. While typing this code, you will notice that LinQ also enjoys full intellisense support from .Net.
There are a few things that you might have noticed with my code:
1. Using the var keyword to declare the collection result – you have probably noticed that I did not write or explicitly declare the datatype for our collection. This is because the var keyword instructs the compiler to infer the datatype. This is called Type Inferencing.
2. The p variable also does not have a datatype. At first look, p seems to have no datatype. In this instance, the type was inferred by the compiler when we typed in the "in people" part of the query. This told the compiler that p is to be the same type as people.
3. I used the DateTime object within the LinQ query. LinQ, as I have mentioned is fully integrated with the language of your choice. Whether VB or C#, LinQ will enjoy the use of all the namespaces available with .Net like .StartsWith, .EndsWith etc.
The results of a LinQ query is of type IEnumerable which gives us the ability to bind it to a control or another object. Next I will show you how to use LinQ to put data in a combobox by adding this code:
comboBox1.DataSource = result.ToList();
comboBox1.DisplayMember = "Name";
AFTER I bind the datasoruce to the grid.
The LinQ query is actually executed whenever it is really used. During the setting of the DataSource property or the DataBind() method for web applicaitons.
So at this point in the code, the LinQ query is called and performed twice, when we set the DataSource property of the grid, and when we set the datasource property of the combobox.
With this in mind, imagine that in between the time the grid was bound, and when the combobox was bound, our data changed. In this event, the combobox will be bound with the fresher data.
Let’s add another line of code to illustrate this:
Notice that I added a new person, after the collection was used to populate the grid, but before we used the collection to bind the combobox. Notice that the combobox has fresh data.
When I run the program, you will notice that the Grid has three entries but the combobox has four.
And that ends this simple demo to show how to use LinQ on a colection, and some new features that we can use with LinQ.