How LINQ Works

To use LINQ in your program you need a LINQ expression. Then then return type of LINQ expression is an iterator object that implements IEnumerable<T> and enumerating over the iterator object, LINQ performs its work. However this makes some questions about  LINQ

  • How LINQ works?
  • How LINQ execute statements?
  • How LINQ filter results?

LINQ works differently for different type of data type for example if you are using LINQ with database then it open database connections, perform SQL commands and return requested results back to you. In case of LINQ to objects LINQ simply perform uses for each loop to scan through the collection and return result back to you.

LINQ to objects is always slower as compare to comparable for each block, because this overhead is due to additional methods and delegate works. If you want to implement a advance search routine for in memory objects then un this case using LINQ will be a good idea as compare to for each block.

There are some point to remember while using LINQ to objects

  1. LINQ expressions work on objects that implement Enumerable<T> and LINQ
    expressions return objects that implement IEnumerable<T>.

Overview of Language Integrated Query

Language Integrated Query is one of the feature that ship with .Net Framework 3.5. LINQ is a set of language extension that allows developers to write queries using full power programming that they are using, programming language might be C#, VB or any of the .Net supported language.  In a simplest form LINQ defines keywords that a developer can use to build queries. LINQ expressions allows developers to select, sort,filter, group and transform data.

Same LINQ expression can be used with different type of data sources. For example, LINQ to Objects, the simplest form of LINQ, allows you to query collections of in-memory objects. LINQ to DataSet performs the same feat with the in-memory DataSet. Even more interesting are the two LINQ flavors that let you access external data: LINQ to SQL, which allows you to query a SQL Server database without writing data access code, and LINQ to XML, which allows you to read an XML file without using .NET’s specialized XML classes.

Let’s start with how LINQ work with in memory collections. This is called LINQ to objects or simple LINQ. LINQ to objects allows developers to eliminate logic of for each loop.

For example, imagine you want to get a list of all website that have a word .net

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        ' Get the full collection of website from a helper method.
        Dim websitelist As List(Of Website) = Website.getWebsite
        'find a mathching website
        Dim matches As List(Of Website) = New List(Of Website)
        For Each websitename As Website In websitelist
            If websitename.websitename.Contains(".net") Then
                matches.Add(websitename)
            End If
        Next
    End Sub

Now perform the same task using LINQ

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    Dim websites As New List(Of Website)
    Dim matches As IEnumerable(Of Website)
    matches = From websitename In websites Where websitename.websitename.Contains(".net") Select websitename

End Sub

Website Class Code

Public Class Website
    Public websitename As String
    Public websiteurl As String

    Public Sub New(ByVal websitename As String, ByVal websiteurl As String)
        Me.websitename = websitename
        Me.websiteurl = websiteurl

    End Sub
    Public Shared Function getWebsite() As List(Of Website)
        Dim weblist As New List(Of Website)
        Dim web1 As New Website("Microsoft", "http://microsoft.com")
        weblist.Add(web1)
        Dim web2 As New Website("MSDN", "http://msdn.microsoft.com")
        weblist.Add(web2)
        Dim web3 As New Website("All About ASP.Net", "http://allaboutasp.net")
        weblist.Add(web3)
        Dim web4 As New Website("ReadersZone", "http://readerszone.com")
        weblist.Add(web4)
        Dim web5 As New Website("ASP.net", "http://asp.net")
        weblist.Add(web5)
        Dim web6 As New Website("Silverlight", "http://silverlight.net")
        weblist.Add(web6)
        Return (weblist)

    End Function

End Class

Public Class Website
    Public websitename As String
    Public websiteurl As String

    Public Sub New(ByVal websitename As String, ByVal websiteurl As String)
        Me.websitename = websitename
        Me.websiteurl = websiteurl

    End Sub
    Public Shared Function getWebsite() As List(Of Website)
        Dim weblist As New List(Of Website)
        Dim web1 As New Website("Microsoft", "http://microsoft.com")
        weblist.Add(web1)
        Dim web2 As New Website("MSDN", "http://msdn.microsoft.com")
        weblist.Add(web2)
        Dim web3 As New Website("All About ASP.Net", "http://allaboutasp.net")
        weblist.Add(web3)
        Dim web4 As New Website("ReadersZone", "http://readerszone.com")
        weblist.Add(web4)
        Dim web5 As New Website("ASP.net", "http://asp.net")
        weblist.Add(web5)
        Dim web6 As New Website("Silverlight", "http://silverlight.net")
        weblist.Add(web6)
        Return (weblist)

    End Function

End Class