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
Hi,
Not sure that this is true:), but thanks for a post.
Have a nice day
Dirnov
very helpful