.NET
  Home arrow .NET arrow Page 3 - 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 - Implicitly Typed Variables and Anonymous Types


    (Page 3 of 4 )

    LINQ is where implicitly typed variables shine. Notice how we stored the results in an IEnumerable<Person>. This, of course, works fine, but we're able to shorten it a bit:


    var overThirty = from p in people

     where p.Age > 30

     select p;


    Dim overThirty = From p In people _

     Where p.Age > 30 _

     Select p


    Notice how we return the results as Person objects. This may be appropriate in our example, but consider the case of a relational database or an XML file. We need to represent each piece of data as an object, and we also may not need all of the fields. While in the above example we already had an obvious type handy to represent our data, we could also have used an anonymous type to represent our data.

    Anonymous types are used like this:


    var mike = new { Name = "Mike", Age = 51, Phone = "555-232-2341" };


    Dim mike = New With {.Name = "Mike", .Age = 51, .Phone = "555-232-2341"}


    Anonymous types make sense within the context of LINQ. The following code yields the same results (or, rather, approximately the same, as we'll soon see) as the previous code, but it uses an anonymous type:


    var overThirty = from p in people

     where p.Age > 30

     select new { Name = p.Name, Age = p.Age, Phone = p.Phone };


    Dim overThirty = From p In people _

     Where p.Age > 30 _

     Select New With {.Name = p.Name, .Age = p.Age, .Phone = p.Phone}


    Now, instead of getting a collection of Person objects as a result, we get a collection of objects of an anonymous type. These have to be treated a bit differently. For example, suppose we wanted to loop over the results and print them out. Before, we could do something like this:


    foreach (Person p in overThirty)

    {

     Console.WriteLine("Name: {0}", p.Name);

     Console.WriteLine("Age: {0}", p.Age);

     Console.WriteLine("Phone: {0}", p.Phone);

    }


    For Each p As Person In overThirty

     Console.WriteLine("Name: {0}", p.Name)

     Console.WriteLine("Age: {0}", p.Age)

     Console.WriteLine("Phone: {0}", p.Phone)

    Next


    Using the anonymous type, though, we can't treat each element as a Person object. We can't match it to a specific type, but that's okay because we know the type's properties and can access them in the same way:


    foreach (var p in overThirty)

    {

     Console.WriteLine("Name: {0}", p.Name);

     Console.WriteLine("Age: {0}", p.Age);

     Console.WriteLine("Phone: {0}", p.Phone);

    }


    For Each p In overThirty

     Console.WriteLine("Name: {0}", p.Name)

     Console.WriteLine("Age: {0}", p.Age)

     Console.WriteLine("Phone: {0}", p.Phone)

    Next


    The only difference is that in C#, Person has been replaced with var, and in VB.NET, the type declaration has been taken out entirely.

    In the above example, we chose to make our anonymous type's properties mirror those of the real type, Person. However, we could have made them different. For example, if we only need the name and number, then we're free to leave the Age property out completely, or if we want to add a new property, such as the year in which the person was born, then we're free to add that and then compute the value of it. Anonymous types are especially useful when the data source provides numerous fields, of which you only need to use a few.

    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 4 hosted by Hostway
    Stay green...Green IT