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 |
| 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 |
| 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 |

| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. | ![]() |