Using LINQ to Objects in VB


Server Intellect


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

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


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.

Posted 02/19/2009 at 10:26 PM
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.

Posted 12/18/2009 at 4:27 AM
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.

Posted 05/04/2010 at 9:01 PM
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.

Posted 07/10/2010 at 2:09 AM
Technology Blog said:

So this is LINQ. Looks really useful. I bet you can save lots of programming time.

Posted 07/18/2010 at 5:32 PM
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

Posted 07/20/2010 at 6:31 AM
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

Posted 07/22/2010 at 4:23 PM
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

Posted 07/22/2010 at 4:24 PM
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

Posted 07/22/2010 at 4:25 PM
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

Posted 07/26/2010 at 3:31 AM
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

Posted 07/26/2010 at 7:37 AM
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.

Posted 07/30/2010 at 5:54 AM
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.

Posted 07/31/2010 at 4:49 PM
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.

Posted 07/31/2010 at 4:49 PM
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

Posted 08/02/2010 at 2:57 PM
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

Posted 08/04/2010 at 9:35 AM
wow cd key said:

Thanks for explaining the AJAX in details, i get lots of information about fundamentals of AJAX.

Posted 08/06/2010 at 10:45 AM
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.

Posted 08/08/2010 at 3:24 PM
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. “

Posted 08/09/2010 at 6:08 AM
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:

Posted 08/09/2010 at 12:31 PM
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.

Posted 08/26/2010 at 8:11 AM
louis vuitton bags said:

thank you for sharing

Posted 08/26/2010 at 8:12 AM
louis vuitton bags & lv handbags & louis vuitton & lv said:

else

{

textBox3.Enabled = true;

button4.Text = "Disable >>";

Posted 08/26/2010 at 8:13 AM

Posted 08/26/2010 at 8:14 AM
coach purses said:

cocokathy

Posted 08/26/2010 at 8:16 AM
gucci outlet said:

cocokathy90

Posted 08/26/2010 at 8:16 AM
chanel bags said:

cocokathy90

Posted 08/26/2010 at 8:16 AM
louis vuitton outlet said:

cocokathy90

Posted 08/26/2010 at 8:17 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

Posted 08/26/2010 at 8:17 AM
Supra Shoes said:

cocokathy90

Posted 08/26/2010 at 8:17 AM
gucci handbags said:

cocokathy90

Posted 08/26/2010 at 8:18 AM
Anti Shoes said:

cocokathy

Posted 08/26/2010 at 8:18 AM
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.

Posted 08/26/2010 at 8:20 AM
ugg classic boots & womens ugg boots said:

the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:23 AM
ugg classic boots & ugg boots said:

the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:24 AM
ugg classic boots & ugg boots said:

Let's start with the statusstrip label.Thanks

Posted 08/26/2010 at 8:25 AM
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:

with the statusstrip label.Thanks

Posted 08/26/2010 at 8:27 AM
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

Posted 08/26/2010 at 8:28 AM
coach outlet store online said:
Posted 08/26/2010 at 9:13 PM
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.

Posted 08/30/2010 at 5:21 AM

Leave a Comment