.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT