Using LINQ to Objects in VB

| Using LINQ to Objects in VB |
This tutorial was created with Microsoft Visual Studio .NET 2008. However, if you are using 2005, you can implement LINQ by downloading Microsoft's LINQ Community Technology Preview release from here.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
In this tutorial, we will be looking at using LINQ to Objects. We will be creating a Windows Forms Application that will first define an array of numbers, and then we will use LINQ to Objects to interact with this collection of numbers. We will create buttons to display the results of calculations of the numbers, demonstrating the built-in functions of LINQ, that we can perform on most any collection.
We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example. The label will be to show the results of our functions, and then the other three buttons we will use for LINQ functions.
We will also implement a StatusStrip control to make use of the label within, so that we can manipulate it on the mouse hover and leave events. The form may look something like this:

Once we are done with our form, we can double-click on the buttons in design view to create the click event handlers. We can also create the hover and leave handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events. Let's start with the StatusStrip label. We will change the text on the hover and leave events of each of the buttons to let the user know what each of the buttons does:
| Private Sub butShowNum_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butShowNum.MouseHover
ToolStripStatusLabel1.Text = "Display all numbers in array"
End Sub
Private Sub butShowNum_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butShowNum.MouseLeave
ToolStripStatusLabel1.Text = ""
End Sub
Private Sub butLessThan10_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLessThan10.MouseHover
ToolStripStatusLabel1.Text = "Get numbers less than or equal to 10"
End Sub
Private Sub butLessThan10_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLessThan10.MouseLeave
ToolStripStatusLabel1.Text = ""
End Sub
Private Sub butSum_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSum.MouseHover
ToolStripStatusLabel1.Text = "Get SUM of ALL numbers"
End Sub
Private Sub butSum_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSum.MouseLeave
ToolStripStatusLabel1.Text = ""
End Sub
Private Sub butAverage_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAverage.MouseHover
ToolStripStatusLabel1.Text = "Get average of ALL numbers"
End Sub
Private Sub butAverage_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAverage.MouseLeave
ToolStripStatusLabel1.Text = ""
End Sub |
Next, we will create a method that will create an array of numbers that we can call from all of the buttons:
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
| Private Function loadNumbers() As Integer()
Dim theNumbers() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
Return theNumbers
End Function |
To display all numbers in the array, we use a LINQ query and FOR loop:
Private Sub butShowNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butShowNum.Click
lblResult.Text = ""
Dim theResult = From n In loadNumbers() _
Order By n _
Select n
For Each i As Integer In theResult
lblResult.Text = lblResult.Text & i & " "
Next i
End Sub |
Next, we use a similar structure to select only the numbers from the array that are below or equal to 10. Again, we use LINQ:
Private Sub butLessThan10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLessThan10.Click
lblResult.Text = ""
Dim theResult = From n In loadNumbers() _
Where n <= 10 _
Order By n _
Select n
For Each i As Integer In theResult
lblResult.Text = lblResult.Text & i & " "
Next i
End Sub |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
We can also use LINQ built-in functions on the collection, such as the sum of all the numbers and also the average:
Private Sub butLessThan10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLessThan10.Click
lblResult.Text = ""
Dim theResult = From n In loadNumbers() _
Where n <= 10 _
Order By n _
Select n
For Each i As Integer In theResult
lblResult.Text = lblResult.Text & i & " "
Next i
End Sub
Private Sub butSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSum.Click
lblResult.Text = ""
Dim theResult = loadNumbers().Sum()
lblResult.Text = lblResult.Text + theResult.ToString()
End Sub |
|
| Comments |
Vanessa A.L. said:
You are a lifesaver!! Thank you for putting up this tutorial, because of it, my program is working and I get to keep my sanity for the night.
|
casino bacara said:
There's no doubt that LINQ is one of the headline features of Visual Studio Orcas. I've been trying out some LINQ queries; it's a very interesting new area with massive potential. It seems though that most books and articles available at the moment are C# based, so I thought I'd blog my journey through the Land of LINQ in case any other VB DotNetters might find it useful now or in the future.
|
Essay said:
Through this blog the author gives information on how to use LINQ to Objects in VB. First of all he creates a Windows Forms Application which will define an array of numbers. We will be able to interact with these numbers by using LINQ to Objects. He explains wonderfully well how to do it and after that he moves on to programming part. It is also presented with attention to detail. I certainly have the feeling that this blogger has got the god given gift of explaining the most complex things in the simplest way. This is a wonderful blog which goes a long way in alleviating the fears of programmers in VB.
|
Gifts said:
I've been trying out some LINQ queries; it's a very interesting new area with massive potential. It seems though that most books and articles available at the moment are C# based, so I thought I'd blog my journey through the Land of LINQ in case any other VB DotNetters might find it useful now or in the future.
|
Technology Blog said:
So this is LINQ. Looks really useful. I bet you can save lots of programming time.
|
Prostate Cancer Treatment said:
We will be creating a Windows Forms Application that will first define an array of numbers, and then we will use LINQ to Objects to interact with this collection of numbers.Thanks
|
Hydraulic jacks said:
It seems we can LINQ to everything these days. Here's some resources that will get you started using LINQ with XML. Introduction .NET Language-Integrated Query for XML Data- This MSDN introduction is 44 pages in fairly easy to read detail on LINQ to XML
|
Hydraulic jacks said:
From MSDN: "The LINQ Project 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
|
Hydraulic jack said:
Start with listening to Anders talk about LINQ in Anders Hejlsberg on LINQ and Functional Programming this video and reading the MSDN Introduction to LINQ. If that's too long, perhaps the 5 Minute Intro is more your speed. There's also a good Channel
|
Attorney said:
I've been trying out some LINQ queries; it's a very interesting new area with massive potential. It seems though that most books and articles available at the moment are C# based, so I thought I'd blog my journey through the Land of LINQ in case any other VB DotNetters might find it useful now or in the future.Thanks
|
Attorney said:
There's no doubt that LINQ is one of the headline features of Visual Studio Orcas. I've been trying out some LINQ queries; it's a very interesting new area with massive potential. It seems though that most books and articles available at the moment are C# based, so I thought I'd blog my journey through the Land of LINQ in case any other VB DotNetters might find it useful now or in the future.
Thanks
|
Fold Up Bikes said:
he LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations.
|
Holiday Costumes said:
Start with listening to Anders talk about LINQ in Anders Hejlsberg on LINQ and Functional Programming this video and reading the MSDN Introduction to LINQ. If that's too long, perhaps the 5 Minute Intro is more your speed.
|
Holiday Costumes said:
Start with listening to Anders talk about LINQ in Anders Hejlsberg on LINQ and Functional Programming this video and reading the MSDN Introduction to LINQ. If that's too long, perhaps the 5 Minute Intro is more your speed.
|
Piano Lessons said:
Start with listening to Anders talk about LINQ in Anders Hejlsberg on LINQ and Functional Programming this video and reading the MSDN Introduction to LINQ. If that's too long, perhaps the 5 Minute Intro is more your speed. There's also a good Channel
|
Photo Calendars said:
it seems we can LINQ to everything these days. Here's some resources that will get you started using LINQ with XML. Introduction .NET Language-Integrated Query for XML Data- This MSDN introduction is 44 pages in fairly easy to read detail on LINQ to XML
|
wow cd key said:
Thanks for explaining the AJAX in details, i get lots of information about fundamentals of AJAX.
|
Live Food said:
We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example. The label will be to show the results of our functions, and then the other three buttons we will use for LINQ functions.
|
Buy WoW Account said:
“ Thank you for this blog. Thats all I can say. You most definitely have made this blog into something thats eye opening and important. You clearly know so much about the subject, youve covered so many bases. Great stuff from this part of the internet. Again, thank you for this blog. “
|
How to Salsa Dance said:
Once we are done with our form, we can double-click on the buttons in design view to create the click event handlers. We can also create the hover and leave handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events. Let's start with the StatusStrip label. We will change the text on the hover and leave events of each of the buttons to let the user know what each of the buttons does:
|
coach outlet said:
We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example.
|
louis vuitton bags & lv handbags & louis vuitton & lv said:
else
{
textBox3.Enabled = true;
button4.Text = "Disable >>";
Posted 08/26/2010 at 8:13 AM
|
UGG Bailey Button & UGG Bailey Button Boots said:
handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
Let's start with the statusstrip label.Thanks
|
UGG Bailey Button Triplet said:
handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.
|
ugg classic boots & womens ugg boots said:
the MouseHover and MouseLeave events.
|
ugg classic boots & ugg boots said:
the MouseHover and MouseLeave events.
|
ugg classic boots & ugg boots said:
Let's start with the statusstrip label.Thanks
|
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:
textBox3.Enabled = true;
button4.Text = "Disable >>";
Posted 08/26/2010 at 8:13 AM
|
cheap wow accounts said:
I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.
|
|
|