.NET
  Home arrow .NET arrow Page 2 - An Introduction to LINQ
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

An Introduction to LINQ
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-06-09

    Table of Contents:
  • An Introduction to LINQ
  • VB.NET
  • Implicitly Typed Variables and Anonymous Types
  • More Standard Query Operators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    An Introduction to LINQ - VB.NET


    (Page 2 of 4 )


    And in VB.NET, we can represent this as follows:


    Class Person

     Private _name As String

     Private _age As Integer

     Private _phone As String


     Public Sub New(ByVal name As String, ByVal age As Integer, ByVal phone As String)

    _name = name

    _age = age

    _phone = phone

     End Sub


     Public ReadOnly Property Name() As String

     Get

     Return _name

     End Get

     End Property

     Public ReadOnly Property Age() As Integer

     Get

     Return _age

     End Get

     End Property

     Public ReadOnly Property Phone() As String

     Get

     Return _phone

     End Get

     End Property

    End Class


    Now, let's say that we have various Person objects, all arranged in an array:


    Person bob = new Person("Bob", 35, "555-292-3044");

    Person henry = new Person("Henry", 43, "555-292-5312");

    Person joe = new Person("Joe", 22, "555-232-7222");

    Person chuck = new Person("Chuck", 29, "555-292-1134");


    Person[] people = { bob, henry, joe, chuck };



    Dim bob As Person = New Person("Bob", 35, "555-292-3044")

    Dim henry As Person = New Person("Henry", 43, "555-292-5312")

    Dim joe As Person = New Person("Joe", 22, "555-232-7222")

    Dim chuck As Person = New Person("Chuck", 29, "555-292-1134")


    Dim people() As Person = {bob, henry, joe, chuck}


    Suppose we wanted to extract a subset of our array. For example, we can create a collection containing only people over the age of thirty. To do this, we could loop over the array, checking each element's Age property. This approach, however, is made unnecessary by LINQ and, furthermore, is ugly by comparison. So, let's get started with LINQ. Here is how to query the array, obtaining all elements whose Age property is more than thirty:


    IEnumerable<Person> overThirty = from p in people

     where p.Age > 30

     select p;


    Dim overThirty As IEnumerable(Of Person) = From p In people _

     Where p.Age > 30 _

     Select p



    Above, we simply look through each Person object, represented as p, in the array and select, or pull out, each element whose Age property is over thirty. To do this, we use the From, Where and Select operators. From identifies where we're looking, Where sets the conditions we're testing, and Select pulls out the results in the form we want (here, we just pull out the results as Person objects).

    The resulting code using LINQ is, obviously, much more concise and elegant than the alternative, and it's a lot more readable as well. From a casual glance, one can tell exactly what's being done.

    Note that the query syntax is just shorthand for the following:


    IEnumerable<Person> overThirty = people

    .Where(p => p.Age > 30)

     .Select(p => p);


    Dim overThirty As IEnumerable(Of Person) = people _

     .Where(Function(p) p.Age > 30) _

     .Select(Function(p) p)


    Above, the operators are translated into methods. For each operator, we need a function that will perform the relevant task. For example, with Where, we need a function that will determine if p.Age is more than thirty. Lambda expressions are used to provide functions here. They are similar to anonymous functions and were added to support LINQ.

    More .NET Articles
    More By Peyton McCullough


       · Hello, all,This article is the first of two articles intended to be an...
     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek