ARTICLE

Linq introduction in VB.NET

Posted by Rohatash Kumar Articles | LINQ with VB.NET March 24, 2011
This article is a basic introduction to LINQ.
 
Reader Level:

This article is a basic introduction to LINQ.

What is the Linq

LINQ uses an SQL-like syntax to make query expressions well beyond the capabilities of embedded SQL as implemented in programming languages. LINQ is 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 libraries to take advantage of these capabilities.

  • Linq allows us to query a collection implementing IEnumerable<T> with SQL like syntax.
  • We can write condition criteria, do the sorting and so on.
  • A Linq query has two parts. The first is data and the other is query operators.
  • The data could be an array, a list.
  • The operators are defined as static extension methods in System.Linq.Enumerable.
  • LINQ also supports data from a remote server, such as a SQL Server.

Lambda and Linq

Most query operators accept a lambda expression as an argument.which provide developers with a convenient way to write functions that can be passed as arguments for subsequent evaluation. Lambda expressions are similar to CLR delegates and must adhere to a method signature defined by a delegate type.

For example

C# code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication20

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] names = { "VisualBasic", "Java", "Csharp", "Javascript" };

            IEnumerable<string> filteredNames = System.Linq.Enumerable.Where(names, n => n.Length >= 4);

            foreach (string n in filteredNames)

                Console.WriteLine(n);

 

        }

    }

}

In example above, the lambda expression is as follows:

n => n.Length >= 4

VB code

Module Module1

    Sub Main()

        Dim names As String() = {"Visual Basic", "Java", "Csharp", "Javascript"}

        Dim filteredNames As IEnumerable(Of String) = System.Linq.Enumerable.Where(names,

Function(n) n.Length>= 4)

        For Each n As String In filteredNames

            Console.WriteLine(n)

        Next

    End Sub

End Module

OUTPUT

a2.gif

Query expression syntax

C# provides another syntax for writing queries, called query expression syntax.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

class Program

{

    static void Main()

    {

        string[] names = { "VisualBasic", "Java", "C#", "Javascript" };

 

        IEnumerable<string> filteredNames = from n in names select n;

        foreach (string name in filteredNames)

            Console.WriteLine(name);

 

    }

}

 

VB code

 

Module Module1

    Sub Main()

        Dim names As String() = {"VisualBasic", "Java", "C#", "Javascript"}

        Dim filteredNames As IEnumerable(Of String) = From n In names n

        For Each name As String In filteredNames

            Console.WriteLine(name)

        Next

    End Sub

End Module

 

OUTPUT


a2.gif

 

Lambda Expressions and Linq Operators

 

In example below, we are using the following lambda expression to the Where operator:


n => n.Contains ("a")

The input Input type is string, and the return type is bool.

The lambda expression depends on the particular query operator.

 

C# code

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

class Program

{

    static void Main()

    {

        string[] names = { "C", "Java", "C#", "Javascript" };

 

        IEnumerable<string> query = names

        .Where(n => n.Contains("a"))

        .OrderBy(n => n.Length)

        .Select(n => n.ToUpper());

 

        foreach (string name in query)

            Console.WriteLine(name);

    }

}

 

VB code

 

Module Module1

    Sub Main()

        Dim names As String() = {"C", "Java", "C#", "Javascript"}

 

        Dim query As IEnumerable(Of String) = names.Where(Function(n) n.Contains("a")).OrderBy(Function(n) n.Length).[Select](Function(n) n.ToUpper())

 

        For Each name As String In query

            Console.WriteLine(name)

        Next

    End Sub

End Module

 

OUTPUT

a3.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor