.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  
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 - 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

    - 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek