VB.NET LINQ - Introduction

April 18, 2018 Oclemy VB.NET LINQ, VB.NET Array 1 minute, 25 seconds

LINQ stands for Language Intergrated Query. It is pronounced link.

Let's start by getting our feet wet with a Hello World LINQ program to query an array for documents ending with .doc extension in VB.NET.

Module Module1
    Sub Main()
        Dim documents() As String = {"Alpha Centauri in 20 years.doc", "Spaceship Design.ppt", "Project Proposal.doc", "Achieving Light Speed.doc", "Cities of the Future.pdf", "Designing a Language.pdf"}

        Dim wordDocuments = From d In documents
                          Where d.EndsWith("doc")
                          Select d
        For Each document In wordDocuments
            Console.WriteLine(document)
        Next
        Console.ReadLine()
    End Sub
End Module

Result

Alpha Centauri in 20 years.doc
Project Proposal.doc
Achieving Light Speed.doc

Now look at this statement From d In documents Where EndsWith("doc") Select d, doesn't it look like SQL statement instead of VB.NET language syntax?

Yet it's totally valid and very powerful. This is LINQ, a technology that allows us to query data and objects in high level languages like VB.NET and C#.

LINQ introduces query into the VB.NET and C# languages as first-class citizens.

This allows the compiler to type check the LINQ queries.Moreover the LINQ is syntax highlighted and we also get intellisense suggestions.

LINQ as a technology allows developers to write strongly typed and logically structured syntax for data querying.

Before the introduction of LINQ, there was no native syntax for querying data in VB.NET and C#. In fact, LINQ was a game changer not only in the .NET world but also in the wider programming field, at least among the most commonly used programming languages.

Not only can you use LINQ to query collections, but also you can use the to query SQL databases and XML files.

Best Regards.

Comments