KnowDotNet NetRefactor

VS2008 - Using LINQ to Query From an Array

Query Objects Using LINQ in VS2008

by Les Smith
Print this Article Discuss in Forums

What's new in VS2008?  Querying objects with LINQ adds real power to the developer's arsenal of tools for code production.  

The most time consuming part of any application is usually developing code that manipulates data.  If you thought about "manipulating data" before the advent of Visual Studio .NET, you prrobably thought of data from database or files.  With the coming of .NET, data moved to Objects and manipulation of data began to include Objects, Arrays, ArrayLists, Collections, XML, etc.

With the release of VS 2008 comes a whole new paradigm of development tools.  One of the sweetest is LINQ.  Data can be encapsulated in all of the previous containers and forms, but LINQ gives us power in extracting and manipulating data in these containers.  Obviously, we had multiple ways to find and manipulate data for all of the aforementioned objects and forms prior to the advent of LINQ.  However, LINQ is an attempt to give developers a consistent methodology for manipulating internal data that is much like SQL is used to manipulate database data.

In this article, I will create a simple application of LINQ to whet your appetite.  
Click here to see C# Version.I will create a Console Application.  Sub Main is shown below.  It creates an array of names and calls two methods; one pulling data from an array in the convention way and the second using LINQ.

    Sub Main()
      
Dim names() As String = _
         {
"Smith", "Jones", "Samson", "Doe", "Sanders", "Frank"}
      QueryArrayTheOldWay(names)
      Console.WriteLine(
" ")
      LinqQueryStrings(names)
  
End Sub

Next, we have a method for pulling the data from the array using conventional methods.  I loop through the array and build the subset ArrayList using standard code.  I simply want to pull all of the names from the array that start with "S".

   Private Sub QueryArrayTheOldWay(ByVal names As String())
      
Dim sList As New ArrayList

      
' create an arraylist of names beginning with "S"
      Console.WriteLine("Do it the old way")
      
For Each s As String In names
        
If s.Substring(0, 1).Equals("S") Then
            sList.Add(s)
        
End If
      Next

      For i As Integer = 0 To sList.Count - 1
         Console.WriteLine(
"Name {0} beginning with S: " & sList(i), _
            
CType(i + 1, String))
      
Next
   End Sub

Now, the second method will use one line of LINQ code to build the array instead of the For Loop shown above.  Notice that the sList has been changed from an ArrayList to a Generic List of IEnumerable(Of String).

   Private Sub LinqQueryStrings(ByVal names As String())


      
' create an arraylist of names beginning with "S"
      Dim sList As IEnumerable(Of String) = _
        
From s In names Where s.Substring(0, 1).Equals("S")

      
' print the subset of names
      Console.WriteLine("Using LINQ")
      
For i As Integer = 0 To sList.Count - 1
         Console.WriteLine(
"Name {0} beginning with S: " & sList(i), _
            
CType(i + 1, String))
      
Next
      Console.ReadLine()

  
End Sub

In the second example, the subset List is created with only one line of code.  When I run the application, the results are displayed in the Console Window below.  The output from both methods are in the window.

Linq Results


I will show more uses of LINQ as time permits.

Have you tried our newest product, Visual Class Organizer?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable time typing hundreds of braces {} daily needlessly.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  


Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing